南陽oj 題目32 組合數
描述 找出從自然數1、2、... 、n(0
- 輸入
- 輸入n、r。
- 輸出
- 按特定順序輸出所有組合。
特定順序:每一個組合中的值從大到小排列,組合之間按逆字典序排列。 - 樣例輸入
- 5 3
- 樣例輸出
- 543
542
541
532
531
521
432
431
421
321
#include<iostream>
using namespace std;
int c[10],n,r;
void dfs(int cur,int select)
{
if(select==r)
{
for(int i=0;i<select;i++)
cout<<c[i];
cout<<endl;
return;
}
if(cur==n) return;
c[select]=n-cur;
dfs(cur+1,select+1);
dfs(cur+1,select);
}
int main()
{
cin>>n>>r;
dfs(0,0);
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.