문제 해설

 

가능한 부분 수열의 합을 모두 벡터에 넣어주고 벡터를 정렬한 후 unique함수를 활용하여 중복을 배열의 가장 뒤로 보내주었습니다. 이후 인덱스를 비교하여 가장 작은 자연수를 출력시켜 주었습니다.

 

 

코드

 

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
 
    int n;
    int d[20];
    cin >> n;
    vector <int> v;
 
    for (int i = 0; i < n; i++) {
        cin >> d[i];
    }
 
    v.push_back(0);
    v.push_back(d[0]);
 
    for (int i = 1; i < n; i++) {
        int x = v.size();
        for (int j = 0; j < x; j++) {
            v.push_back(v[j] + d[i]);
        }
    }
 
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());
 
 
    for(int i = 1; i <= v.size(); i++){
        if (i == v.size()) {
            cout << i << '\n';
        }
        else if(v[i] != i){
            cout << i << '\n';
            break;
        }
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/14225

 

14225번: 부분수열의 합

수열 S가 주어졌을 때, 수열 S의 부분 수열의 합으로 나올 수 없는 가장 작은 자연수를 구하는 프로그램을 작성하시오. 예를 들어, S = [5, 1, 2]인 경우에 1, 2, 3(=1+2), 5, 6(=1+5), 7(=2+5), 8(=1+2+5)을 만들 수 있다. 하지만, 4는 만들 수 없기 때문에 정답은 4이다.

www.acmicpc.net

 

+ Recent posts