[백준 2309번 일곱 난쟁이]

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<int> picked;
vector<int> smallMan;
bool finish = true;
void findSmallMan(vector<int>& picked, int start ,int tallSum)
{
    if (finish == falsereturn;    // 기저사례. 이미 1번 출력했다면 함수를 빠져나온다.
    if (picked.size() == 7)
    {
        if (tallSum == 100)
        {
            sort(picked.begin(), picked.end());
            for (int i = 0; i < 7; i++)
                cout << picked[i] << endl;
            finish = false;
        }
    }
    for (int i = start; i < 9; i++)    // 순서만 다른 조합을 배제하기 위해 start부터 시작
    {
        picked.push_back(smallMan[i]);
        findSmallMan(picked, i + 1, tallSum + smallMan[i]);
        picked.pop_back();
    }
}
int main(void)
{
    for (int i = 0; i < 9; i++)
    {
        int tall;
        cin >> tall;
        smallMan.push_back(tall);
    }
    findSmallMan(picked, 00);
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

알고리즘 200일 프로젝트 - 5day

원소의 수가 9개로 제한되어 있기때문에 완전탐색 알고리즘을 적용하여 간단하게 해결할 수 있는 문제였다.

 

'알고리즘 > BOJ' 카테고리의 다른 글

백준 6603번: 로또  (0) 2020.04.12
백준 7568번: 덩치  (0) 2020.04.11
백준 14502번: 연구소  (0) 2020.04.11
백준 2231번: 분해합  (0) 2020.04.11
백준 14501번: 퇴사  (0) 2020.04.10

+ Recent posts