4gats 2023. 3. 20. 19:49

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

 

6603번: 로또

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로

www.acmicpc.net

브루트포스 재귀의 정석 같은 문제

1. 정답 -> cnt == 6

2. 불가능한 경우

    index == d.size()

3. 다음 경우

    선택   go(d, index + 1, cnt + 1)

    선택 x go(d, index + 1, cnt)

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
44
#include <iostream>
#include <vector>
 
using namespace std;
vector<int> lotto;
void go(vector<int> &d, int index, int cnt)
{
    if (cnt == 6)
    {
        for (int num : lotto)
            printf("%d ", num);
        printf("\n");
        return;
    }
 
    // 초과
    if (d.size() == index) 
        return;
 
    lotto.push_back(d[index]);
    go(d, index + 1, cnt + 1);
    lotto.pop_back();
    go(d, index + 1, cnt);
}
 
int main()
{
    int n;
    
    while (1)
    {
        cin >> n;
        if (n == 0)
            break;
 
        vector<int> d(n);        
        for (int i = 0; i < n; i++)
            cin >> d[i];
 
        go(d, 0, 0);
        cout << '\n';
    }
    return 0;
}
cs

사전순이 되는 이유

오름차순을 입력 받았고

lotto.push_back(d[index]);

시작부터 넣었으므로