알고리즘
[C++] 백준 6603번 로또
J3SUNG
2019. 8. 25. 18:10
728x90
DFS로 문제를 풀었습니다.
로또를 할 때 몇개의 번호로 조합가능한 수들을 출력하는 문제입니다.
DFS를 통해서 각 숫자에서 깊이가 6이 될때까지 조합을 만듭니다.
만든 조합들은 전부 출력합니다.
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n;
int arr[20];
int com[20];
void DFS(int next, int depth)
{
if (depth == 6) {
for (int i =0; i < 6; ++i) {
cout << com[i] << ' ';
}
cout << '\n';
return;
}
for (int i = next; i < n; ++i) {
com[depth] = arr[i];
DFS(i + 1, depth + 1);
}
}
int main(int argc, char *argv[])
{
while (1) {
cin >> n;
if (n == 0) {
break;
}
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
DFS(0, 0);
cout << '\n';
}
return 0;
}