递归实现从n个数中选r个数的组合数

 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.
  • 不要因为结束而哭泣。微笑吧,因为你曾拥有。

转载于:https://www.cnblogs.com/wjf0/p/8098969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值