주어진 집합에서 수 6개를 고르는 경우의 수를 완전탐색하는 문제다.
사전순 출력이기 때문에 dfs로 순차 출력하면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream> using namespace std; int k, arr[15], ans[15]; void dfs(int s, int c) { if(c == 6) { for(int i = 0; i < 6; i++) cout << ans[i] << " "; cout << endl; } else for(int i = s; i < k; i++) { ans[c] = arr[i]; dfs(i + 1, c + 1); } } int main() { while(cin >> k && k) { for(int i = 0; i < k; i++) cin >> arr[i]; dfs(0, 0); cout << endl; } }
|
구조 자체가 이해했더라도 갑자기 떠올리기 힘든 형태다ㅠ