Describe
Find all combinations of the number of R (0<r<=n) from the natural number 1, 2 、... 、 n (0<n<10).
-
-
Input
-
-
Enter N, R.
Output
output all combinations in a specific order.
Specific order: The values in each combination are arranged from large to small, and the combinations are sorted in inverse dictionary order.
Sample input
5 3
Sample output
543542541532531521432431421321
This is about DFS, just beginning is not too difficult, I began the idea is relatively simple, later found unable to jump out of the loop body, can not go on, no way can only change methods, and then with reference to other people's code to AC
#include <iostream> #include <string.h> #include <string>using namespace std;int a[100];void dfs (int n , int r) {int i,j;for (i=n;i>=r;i--) {a[r]=i;if (r>1) DFS (i-1,r-1); Else{for (j=a[0];j>0;j--) cout<<a[j]; Cout<<endl;}}} int main () {int N,r;while (cin>>n>>r) {A[0]=r;dfs (n,r);} return 0;}
Number of combinations