描述
-
找出从自然数1、2、... 、n(0<n<10)中任取r(0<r<=n)个数的所有组合。
输入
-
输入n、r。
输出
-
按特定顺序输出所有组合。
特定顺序:每一个组合中的值从大到小排列,组合之间按逆字典序排列。
样例输入
-
5 3
样例输出
-
543
542
541
532
531
521
432
431
421
321
本质是递归求解,通过判断r与0比较来安排出递归次数。
1 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4 int a[11];
5 int m,n;
6 int dfs(int x,int y)
7 {
8 int i,j;
9 if(y==0)
10 {
11 for(i=m;i>=1;i--)
12 cout<<a[i];
13 cout<<endl;
14 }
15 else
16 {
17 for(i=x;i>=y;i--)
18 {
19 a[y]=i;
20 dfs(i-1,y-1);
21 }
22 }
23 }
24 int main()
25 {
26 while(cin>>n>>m)
27 {
28 dfs(n,m);
29 }
30 }
转载于:https://www.cnblogs.com/a1225234/p/4542429.html