NYOJ 组合数

来源:互联网 发布:尚学堂 js 编辑:程序博客网 时间:2024/06/05 09:20

描述
找出从自然数1、2、… 、
n中任取r个数的所有组合。
输入
输入n、r。
输出
按特定顺序输出所有组合。
特定顺序:每一个组合中的值从大到小排列,组合之间按逆字典序排列。
样例输入
5 3
样例输出
543
542
541
532
531
521
432
431
421
321

题目地址
http://acm.nyist.net/JudgeOnline/problem.php?pid=32%20%E9%A2%98%E7%9B%AE%E5%9C%B0%E5%9D%80

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;int n, r, f[10];void dfs(int x, int y){    if(y == r + 1)//找到一个数    {        for(int i = 1; i <= r; ++i)        {            cout << f[i];        }        cout << endl;    }    for(int i = x; i > 0; --i)     {           f[y] = i;         dfs(i - 1, y + 1); // 下一个位置 i-1代表低位的数字小于高位的数字 y+1代表下一位    }}int main(){    cin >> n >> r;    dfs(n, 1);    return 0;}
5 0