Number of combinations

Source: Internet
Author: User

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

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.