题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1089
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 chanceof winning - is to select a subset S containingk (k>6) of these 49 numbers, and then play several games with choosing numbers only from S.For example, fork=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 theset S and then prints all possible games choosing numbers only from S.
Input Specification
The input file will contain one or more test cases. Each test case consistsof 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 Specification
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 separatedfrom each other by exactly one space. The games themselves have to besorted lexicographically, that means sorted by the lowest number first, thenby 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 7 8 1 2 3 5 8 13 21 34 0
Sample Output
1 2 3 4 5 6 1 2 3 4 5 7 1 2 3 4 6 7 1 2 3 5 6 7 1 2 4 5 6 7 1 3 4 5 6 7 2 3 4 5 6 7 1 2 3 5 8 13 1 2 3 5 8 21 1 2 3 5 8 34 1 2 3 5 13 21 1 2 3 5 13 34 1 2 3 5 21 34 1 2 3 8 13 21 1 2 3 8 13 34 1 2 3 8 21 34 1 2 3 13 21 34 1 2 5 8 13 21 1 2 5 8 13 34 1 2 5 8 21 34 1 2 5 13 21 34 1 2 8 13 21 34 1 3 5 8 13 21 1 3 5 8 13 34 1 3 5 8 21 34 1 3 5 13 21 34 1 3 8 13 21 34 1 5 8 13 21 34 2 3 5 8 13 21 2 3 5 8 13 34 2 3 5 8 21 34 2 3 5 13 21 34 2 3 8 13 21 34 2 5 8 13 21 34 3 5 8 13 21 34
这道题有好几种解法,我用的纯暴力,因为k比较小,6个for。。。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<queue>
#define max(x,y)x>y?:xy
#define min(x,y)x<y?x:y
using namespace std;
const int maxn=1005;
int a[maxn];
int main()
{
//freopen("in.txt","r",stdin);
int n;
int ca=1;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
if(ca>1)
printf("\n");
for(int i=0;i<n-5;i++)
for(int j=i+1;j<n-4;j++)
for(int k=j+1;k<n-3;k++)
for(int l=k+1;l<n-2;l++)
for(int p=l+1;p<n-1;p++)
for(int q=p+1;q<n;q++)
printf("%d %d %d %d %d %d\n",a[i],a[j],a[k],a[l],a[p],a[q]);
printf("\n");
}
return 0;
}
参考别人的DFS
代码:
#include<stdio.h>
#include<string.h>
const int maxn=20;
int a[maxn];
int ans[10];
int cnt;
int k;
void dfs(int i,int num)
{
if(i>k)
return;
if(num==6)
{
printf("%d",ans[0]);
for(int i=1;i<6;i++)
printf(" %d",ans[i]);
printf("\n");
return;
}
ans[cnt++]=a[i];
dfs(i+1,num+1);
cnt--;
dfs(i+1,num);
}
int main()
{
//freopen("in.txt","r",stdin);
int ca=0;
while(scanf("%d",&k),k)
{
memset(ans,0,sizeof(ans));
cnt=0;
for(int i=0;i<k;i++)
scanf("%d",&a[i]);
if(ca++)
printf("\n");
dfs(0,0);
}
return 0;
}
这里还有人用STL写的
代码:
/*0.009s*/
#include<bits/stdc++.h>
using namespace std;
const int mx = 15;
int a[15];
bool ok[15];
/*
输出从n个数中选m个数的组合字典序
以n=7,m=3为例
首先构造ok=1110000
然后ok的前一个排列为1101000
再前一个排列为1100100...1100001
然后是1011000...直到0000111
*/
void printfC(int n, int m)
{
int i;
for (i = 0; i < m; ++i) ok[i] = true;
for (; i < n; ++i) ok[i] = false;
do
{
for (i = 0; i < n; ++i)
if (ok[i]) {printf("%d", a[i]); break;}
for (++i; i < n; ++i)
if (ok[i]) printf(" %d", a[i]);
putchar(10);
}
while (prev_permutation(ok, ok + n));
}
int main()
{
int n, i;
bool ok = false;
while (scanf("%d", &n), n)
{
if (ok) putchar(10);
else ok = true;
for (i = 0; i < n; ++i) scanf("%d", &a[i]);
printfC(n, 6);
}
return 0;
}