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.
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 34 3 5 8 13 21 34
全排列,emmm,PE很恶心,要注意
#include <stdio.h> #include <string.h> int N; int v[106]; int a[15]; int b[10]; void dfs(int x) { if(x > 6) return ; if(x == 6) { for(int i = 0; i < 6; i++) { printf("%d",b[i]); if(i != 5) printf(" "); } printf("\n"); } for(int i = 1; i <= N; i++) { if(v[a[i]] == 0) { if(x > 0 && a[i] < b[x-1]) continue; b[x] = a[i]; v[a[i]] = 1; dfs(x+1); v[a[i]] = 0; } } } int main() { int k = 0; while(~scanf("%d",&N) && N) { memset(v, 0, sizeof(v)); for(int i = 1; i <= N; i++) scanf("%d",&a[i]); if(k) printf("\n"); else k = 1; dfs(0); } return 0; }