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,13,
1,2,3,5,8,21
1,2,3,5,8,21,
1,2,3,5,8,34
1,2,3,5,8,34,
1,2,3,5,13,21
1,2,3,5,13,21, ...
3,5,8,13,21,34
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.
7 1 2 3 4 5 6 7 8 1 2 3 5 8 13 21 34 0
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 343 5 8 13 21 34
#include<iostream>
#include<cstdio> #include<cstring> using namespace std; int n,a[15]; int vis[15]; void DFS(int num,int k) { int i; if(k==6) { int ii=0; for(i=0; i<n; i++) { if(vis[i]) { ii++; if(ii==6) printf("%d\n",a[i]); else printf("%d ",a[i]); } } return ; } for(i=num+1; i<n; i++) { vis[i]=1; DFS(i,k+1); vis[i]=0; } } int main() { int i,l; int kk=0; while(scanf("%d",&n) && n) { if(kk!=0) printf("\n"); for(i=0; i<n; i++) scanf("%d",&a[i]); for(i=0; i<=n-6; i++) { memset(vis,0,sizeof(vis)); vis[i]=1; DFS(i,1); } kk++; } }