HDU 1342 Lotto 【DFS】
来源:互联网 发布:白城广逸网络地点 编辑:程序博客网 时间:2024/04/30 22:55
Lotto
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1514 Accepted Submission(s): 751
Problem Description
In a Lotto I have ever played, one has 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.
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 file 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
7 1 2 3 4 5 6 78 1 2 3 5 8 13 21 340
Sample Output
1 2 3 4 5 61 2 3 4 5 71 2 3 4 6 71 2 3 5 6 71 2 4 5 6 71 3 4 5 6 72 3 4 5 6 71 2 3 5 8 131 2 3 5 8 211 2 3 5 8 341 2 3 5 13 211 2 3 5 13 341 2 3 5 21 341 2 3 8 13 211 2 3 8 13 341 2 3 8 21 341 2 3 13 21 341 2 5 8 13 211 2 5 8 13 341 2 5 8 21 341 2 5 13 21 341 2 8 13 21 341 3 5 8 13 211 3 5 8 13 341 3 5 8 21 341 3 5 13 21 341 3 8 13 21 341 5 8 13 21 342 3 5 8 13 212 3 5 8 13 342 3 5 8 21 342 3 5 13 21 342 3 8 13 21 342 5 8 13 21 343 5 8 13 21 34
深搜
*/
#include<cstdio>#include<cstring>int k,step,a[50],ans[6],visit[50];void dfs(int step){if(step==6) {int flag=1;for(int i=0; i<5; i++) {if(ans[i]>ans[i+1]) {flag=0;break;}}if(flag) {for(int i=0; i<6; i++) {if(!i)printf("%d",ans[i]);elseprintf(" %d",ans[i]);}printf("\n");}}else//else不可缺 { for(int i=0; i<k; i++) {if(!visit[i]) {ans[step]=a[i];visit[i]=1;dfs(step+1);visit[i]=0;}}}}int main(){int flag=0;while(scanf("%d",&k)&&k) {if(flag) printf("\n");flag=1;for(int i=0; i<k; i++)scanf("%d",&a[i]);memset(visit,0,sizeof(visit));dfs(0);}}
0 0
- hdu--1342 lotto (dfs)
- hdu-1342-Lotto(dfs)
- HDU 1342 Lotto 【DFS】
- HDU 1342 Lotto 【DFS】
- HDU 1342 Lotto-DFS
- HDU 1342 Lotto (DFS)
- HDU 1342 Lotto (dfs)
- HDU 1342 Lotto(DFS)
- HDU 1342--Lotto【DFS(递增全排列)】
- 1342 Lotto【dfs】
- hd-1342:Lotto DFS
- hdoj--1342--Lotto(dfs)
- HDOJ.1342 Lotto (DFS)
- hdu 1342 Lotto
- hdu-1342 Lotto
- hdu 1342 Lotto
- hdu-1342 Lotto
- HDU 1342 Lotto
- 前端资源的罗列
- 线程池实例:使用Executors和ThreadPoolExecutor
- 算法导论-----------堆排序研究 (堆排序原理及算法实现(最大堆))
- 异步函数调用 犯错
- 数据仓库和数据集市的区别
- HDU 1342 Lotto 【DFS】
- 双系统安装
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- gentoo软件安装
- Fragment防止自动清理 (ViewPager滑动时,滑出屏幕后被清理)
- C++标准库和命名空间
- Android4.4 Framework分析——getContentResolver启动ContentProvider的过程
- 谈谈C++私有继承
- 消息摘要算法-MAC算法系列