NYOJ 32 组合数

组合数

时间限制: 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<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<iostream>  
#include<string>  
#include<vector>  
#include<stack>  
#include<bitset>  
#include<cstdlib>  
#include<cmath>  
#include<set>  
#include<list>  
#include<deque>  
#include<map>  
#include<queue>  
using namespace std;
int n, r;
int a[15];
void dfs(int num,int count){
	//简单深搜,我们每一级的值是逆字典序查找的,注意里面含有两个判定条件,i>0以及count==r。
	if (count == r) {
		for (int i = 0; i < count; i++)
			cout << a[i];
		cout << endl;
		return;
	}
	for (int i = num; i > 0; i--) {
		a[count] = i;
		dfs(i-1, count+1);
	}

}
int main() {
	cin >> n >> r;
	dfs(n, 0);
	return 0; 
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值