Nyoj 32 combination (deep search, good, good question)
Combination time limit: 3000 MS | memory limit: 65535 KB difficulty: 3
-
Description
-
Find from natural numbers 1, 2,..., n (0
-
Input
-
Enter n and r.
-
Output
-
Output all combinations in a specific order.
Specific order: Values in each combination are arranged in ascending order and in reverse Lexicographic Order.
-
Sample Input
-
5 3
-
Sample output
-
543542541532531521432431421321
-
Source
-
[Miao Dongdong] original
-
Uploaded
Miao Dongdong
Different from my previous method, I used to output all possibilities directly, but now I need to output all three numbers in descending order, in addition, the three numbers of the new output cannot be exactly the same as those of the previous three numbers (I is also considered the same for different orders )! The top and v variables are cleverly used to assign values during changes. The method is good!
The Code is as follows:
#include
int n,r;int a[12];void dfs(int top,int v){int i;if(v==0){for(i=r;i>0;i--){printf("%d",a[i]);}puts("");return ;}for(i=top;i>=v;--i){a[v]=i;dfs(i-1,v-1);}}int main(){scanf("%d%d",&n,&r);dfs(n,r);return 0;}