1 #include <stdio.h>
2 #include <stdlib.h>
3 int a[100], count;
4 void comb(int m, int k)
5 {
6 int i, j;
7 for(i = m; i >= k; --i)
8 {
9 // 用来存储每个组合中的数据
10 a[k] = i;
11 if(k > 1)
12 comb(i - 1, k - 1);
13 else
14 {
15 for(j = a[0]; j > 0; --j)
16 printf("%d ", a[j]);
17 printf("\n");
18 ++ count;
19 }
20 }
21 }
22 // 从n个数中选r个数的组合
23 int main()
24 {
25 int n, r;
26 count = 0;
27 printf("Please input n and r:\n");
28 scanf("%d %d", &n, &r);
29 if(r > n)
30 printf("input n, r error!");
31 else
32 {
33 // a[0]仅仅充当一个变量的作用
34 a[0] = r;
35 comb(n, r);
36 }
37 printf("Total numbers : %d", count);
38 return 0;
39 }
- Don't cry because it is over, Smile because it happened.
- 不要因为结束而哭泣。微笑吧,因为你曾拥有。