출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]

상세 컨텐츠

본문 제목

백준 2309 일곱난쟁이(brute force)

알고리즘

by 장동균 2020. 4. 14. 20:27

본문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <algorithm>
 
const int MAX = 9;
 
int sum;
int height[MAX];
 
using namespace std;
 
void sevenDwarf(int height[9])
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = i + 1; j < 9; j++)
        {
            if (sum - (height[i] + height[j]) == 100)
            {
                for (int k = 0; k < 9; k++)
                {
                    if (k != i && k != j)
                        cout << height[k] << "\n";
                }
                return;
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(Null);
    cout.tie(Null);
    for (int i = 0; i < 9; i++)
    {
        int tmp;
        cin >> tmp;
        sum += tmp;
        height[i] = tmp;
    }
    sort(height, height + 9);
    sevenDwarf(height);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

재귀를 쓰지 않아도 되는 brute force 문제. 확실히 종만북 문제들보다 쉽다. 9명 중에서 해당하지 않는 2명을 빼내면 되기에 2중 for loop를 돌려서 모든 경우를 확인하고 조건에 부합(합이 100)하는 경우 출력하고 프로그램을 종료한다.

관련글 더보기

댓글 영역