组合数
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
- 找出从自然数1、2、... 、n(0<n<10)中任取r(0<r<=n)个数的所有组合。
- 输入
- 输入n、r。 输出
- 按特定顺序输出所有组合。
特定顺序:每一个组合中的值从大到小排列,组合之间按逆字典序排列。 样例输入 -
5 3
样例输出 -
543 542 541 532 531 521 432 431 421 321
-
#include<stdio.h> #include<stdlib.h> int a[15]; void print(int t)//输出 { int i; for (i=0;i<t;i++) printf("%d",a[i]); printf("\n"); } void DFS(int n,int r,int t)//查找储存序列 { if (r==0){ print(t); return; } int i; for (i=n;i>0;i--) { a[t]=i; DFS(i-1,r-1,t+1); } } int main() { int n,r; scanf("%d%d",&n,&r); DFS(n,r,0); return 0; }