Nyoj 32 combination (deep search, good, good question)

Source: Internet
Author: User

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;}
   


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.