์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€ (boj) ํŒŒ์ด์ฌ , C++ - 2309 : ์ผ๊ณฑ ๋‚œ์Ÿ์ด

  • -

๋ฌธ์ œ ๋งํฌ : https://www.acmicpc.net/problem/2309

 

2309๋ฒˆ: ์ผ๊ณฑ ๋‚œ์Ÿ์ด

์•„ํ™‰ ๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๋‚œ์Ÿ์ด๋“ค์˜ ํ‚ค๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ํ‚ค๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š” ์ž์—ฐ์ˆ˜์ด๋ฉฐ, ์•„ํ™‰ ๋‚œ์Ÿ์ด์˜ ํ‚ค๋Š” ๋ชจ๋‘ ๋‹ค๋ฅด๋ฉฐ, ๊ฐ€๋Šฅํ•œ ์ •๋‹ต์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€์ธ ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

 

 

 

 

๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ˜•ํƒœ์˜ ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋ฌธ์ œ์ด๋‹ค.

๋ธŒ๋ฃจํŠธํฌ์Šค๋ž€ ? https://infinitt.tistory.com/240

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ (2) - ๋ธŒ๋ฃจํŠธ ํฌ์Šค(Brute Force)

* ๋ธŒ๋ฃจํŠธ ํฌ์Šค (Brute Force) ๋ฌด์ฐจ๋ณ„ ๋Œ€์ž…ํ•˜์—ฌ ์–ต์ง€๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ฆ‰, ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ex ) ๋„ค์ž๋ฆฌ ๋น„๋ฐ€๋ฒˆํ˜ธ์˜ ์ž๋ฌผ์‡ ๋Š” 0000~9999๋ฅผ ์ „๋ถ€ ๋Œ€์ž…ํ•ด๋ณด..

infinitt.tistory.com

9๋ช…์ค‘์— ํ•ฉ์ด 100์ด๋˜๋Š” 7๋ช…์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

9๋ช…์˜ ํ•ฉ์—์„œ 2๋ช…์„ ๋นผ๋ฉด์„œ 100์ด ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋„๋ก ์ฝ”๋”ฉ์„ ํ–ˆ๋‹ค. 

 

 

์ •๋‹ต์ฝ”๋“œ - C++
#include <iostream>
#include <algorithm>
using namespace std;
int a[9];
int n = 9;
int main(){
    int sum = 0;
    for (int i =0; i<n; i++){
        cin >>a[i];
        sum += a[i];
    }
    sort(a,a+n);

    for (int i=0; i<n; i++){
        for (int j=i+1; j<n; j++){
            if (sum - a[i] - a[j] == 100){
                for (int k=0; k<n; k++){
                    if (i==k || j==k) continue;
                    cout<< a[k] << '\n';
                }
                return 0;
            }

        }
    }
    return 0;
}

 

 

 

์ •๋‹ต์ฝ”๋“œ - ํŒŒ์ด์ฌ
arr = []
#์ž…๋ ฅ
for _ in range(9):   
    arr.append(int(input()))


#๋ธŒ๋ฃจํŠธ ํฌ์Šค : 9๋ช…์ค‘, 2๋ช…์„ ๋บ€ ๋‚˜๋จธ์ง€์˜ ํ•ฉ์ด 100
for i in range(9):
    for k in range(9):
        if sum(arr) - arr[i] - arr[k] == 100:
            remove = [arr[i],arr[k]] #ํ•„์š” ์—†๋Š” 2๋ช…


#์ถœ๋ ฅ
arr.remove(remove[0])  
arr.remove(remove[1])
arr.sort()
for i in arr:
    print(i)

์ถ”๊ฐ€ : 2์ค‘ for๋ฌธ์—์„œ, range์˜ ๋ฒ”์œ„์„ค์ •์„ for k in range(i+1,9) ๋กœ ํ–ˆ์–ด์•ผ, ์ค‘๋ณต์—ฐ์‚ฐ ์—†๋Š” ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค.

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.