nyoj-32 组合数(DFS)

来源:互联网 发布:自古枪兵幸运e 知乎 编辑:程序博客网 时间:2024/05/16 13:40

组合数

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
找出从自然数1、2、... 、n(0<n<10)中任取r(0<r<=n)个数的所有组合。
输入
输入n、r。
输出
按特定顺序输出所有组合。
特定顺序:每一个组合中的值从大到小排列,组合之间按逆字典序排列。
样例输入
5 3
样例输出
543542541532531521432431421

321

# include<stdio.h># include<string.h># include<algorithm>using namespace std;int num[12];int n,r;void dfs(int x,int y){int t;if(y == 0){for(t = r; t >= 1; t--)printf("%d",num[t]); printf("\n");}else    {    for(t = x ;t >= y; t--)    {    num[y] = t;    dfs(t-1,y-1);    }    }}int main(){while(~scanf("%d%d",&n,&r)){dfs(n,r);}return 0;}


0 0