BOJ::6603 로또
https://www.acmicpc.net/problem/6603
백트래킹을 이용하는 문제.
C++에서 스트링,int 변환이 익숙지 않았었는데 이 문제를 풀면서 도움이 많이 되었다.
visited 배열은 굳이 필요하지 않다.
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 <stdio.h> #include <string> using namespace std; int a[13]; int cnt,n; string res; void dfs(int v, string str) { cnt++; if (cnt == 6) { res.append(str + "\n"); } else { for (int i = v + 1; i < n; i++){ dfs(i, str+to_string(a[i])+" "); } } cnt--; } int main() { res = ""; while (true) { scanf("%d", &n); if (n == 0) { break; } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n-5; i++) { cnt = 0; dfs(i, to_string(a[i]).append(" ")); } res.append("\n"); } printf("%s", res.c_str()); } | cs |
'BOJ::문제풀이' 카테고리의 다른 글
7576 토마토 (0) | 2018.01.06 |
---|---|
7562 나이트의 이동 (0) | 2018.01.06 |
6588 골드바흐의 추측 (0) | 2018.01.06 |
5014 스타트링크 (0) | 2018.01.06 |
3187 양치기 꿍 (0) | 2018.01.06 |