POJ 2245 Lotto(水水的dfs)

「爱情、让人受尽委屈。」 2022-06-11 08:59 305阅读 0赞

In the German Lotto you have to select 6 numbers from the set {1,2,…,49}. A popular strategy to play Lotto - although it doesn’t increase your chance of winning - is to select a subset S containing k (k > 6) of these 49 numbers, and then play several games with choosing numbers only from S. For example, for k=8 and S = {1,2,3,5,8,13,21,34} there are 28 possible games: [1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], … [3,5,8,13,21,34].

Your job is to write a program that reads in the number k and the set S and then prints all possible games choosing numbers only from S.

Input

The input will contain one or more test cases. Each test case consists of one line containing several integers separated from each other by spaces. The first integer on the line will be the number k (6 < k < 13). Then k integers, specifying the set S, will follow in ascending order. Input will be terminated by a value of zero (0) for k.

Output

For each test case, print all possible games, each game on one line. The numbers of each game have to be sorted in ascending order and separated from each other by exactly one space. The games themselves have to be sorted lexicographically, that means sorted by the lowest number first, then by the second lowest and so on, as demonstrated in the sample output below. The test cases have to be separated from each other by exactly one blank line. Do not put a blank line after the last test case.

Sample Input

  1. 7 1 2 3 4 5 6 7
  2. 8 1 2 3 5 8 13 21 34
  3. 0

Sample Output

  1. 1 2 3 4 5 6
  2. 1 2 3 4 5 7
  3. 1 2 3 4 6 7
  4. 1 2 3 5 6 7
  5. 1 2 4 5 6 7
  6. 1 3 4 5 6 7
  7. 2 3 4 5 6 7
  8. 1 2 3 5 8 13
  9. 1 2 3 5 8 21
  10. 1 2 3 5 8 34
  11. 1 2 3 5 13 21
  12. 1 2 3 5 13 34
  13. 1 2 3 5 21 34
  14. 1 2 3 8 13 21
  15. 1 2 3 8 13 34
  16. 1 2 3 8 21 34
  17. 1 2 3 13 21 34
  18. 1 2 5 8 13 21
  19. 1 2 5 8 13 34
  20. 1 2 5 8 21 34
  21. 1 2 5 13 21 34
  22. 1 2 8 13 21 34
  23. 1 3 5 8 13 21
  24. 1 3 5 8 13 34
  25. 1 3 5 8 21 34
  26. 1 3 5 13 21 34
  27. 1 3 8 13 21 34
  28. 1 5 8 13 21 34
  29. 2 3 5 8 13 21
  30. 2 3 5 8 13 34
  31. 2 3 5 8 21 34
  32. 2 3 5 13 21 34
  33. 2 3 8 13 21 34
  34. 2 5 8 13 21 34
  35. 3 5 8 13 21 34

题解:

好久没做过那么水的比赛题了qwq

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<deque>
  12. #define M (t[k].l+t[k].r)/2
  13. #define lson k*2
  14. #define rson k*2+1
  15. using namespace std;
  16. int a[20];
  17. int b[20];
  18. int n;
  19. void dfs(int len,int index)
  20. {
  21. int i;
  22. if(len==6)
  23. {
  24. for(i=0;i<6;i++)
  25. {
  26. printf("%d",b[i]);
  27. if(i!=5)
  28. printf(" ");
  29. else
  30. printf("\n");
  31. }
  32. return;
  33. }
  34. if(index>=n||len+n-index<6)//稍微枝剪一下,如果当前长加上后面所有长都小于6直接返回
  35. return;
  36. b[len]=a[index];
  37. dfs(len+1,index+1);//取了该数
  38. dfs(len,index+1);//没取该数
  39. }
  40. int main()
  41. {
  42. int i,j,k,tag=1;
  43. while(scanf("%d",&n)!=EOF&&n)
  44. {
  45. if(!tag)
  46. printf("\n");
  47. for(i=0;i<n;i++)
  48. scanf("%d",&a[i]);
  49. dfs(0,0);
  50. tag=0;
  51. }
  52. return 0;
  53. }

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读

    相关 DFS-求数目

    题目: 有一个大小为 NM 的园子,雨后积起了水。八连通的积水被认为是连接在一起的。请求出园子里总共有多少水洼?(八连通指的是下图中相对 W 的的部分) W