NYOJ 32 组合数 dfs

   神搜枚举所有的情况即可,题目:

组合数

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 3
描述
找出从自然数1、2、... 、n(0
输入
输入n、r。
输出
按特定顺序输出所有组合。
特定顺序:每一个组合中的值从大到小排列,组合之间按逆字典序排列。
样例输入
5 3
样例输出
543
542
541
532
531
521
432
431
421
321
ac代码:

#include 
#include 
#include 
using namespace std;
int num[11],n,r;
void dfs(int x,int y){
	if(y==0){
		for(int i=r;i>=1;--i){
		  printf("%d",num[i]);
		}
		printf("\n");
	}
	else{
		for(int i=x;i>=y;--i){
		  num[y]=i;
		  dfs(i-1,y-1);
		}
	}
}
int main(){
  //freopen("11.txt","r",stdin);
  while(~scanf("%d%d",&n,&r)){
	dfs(n,r);
  }
  return 0;
}


你可能感兴趣的:(搜索)