C + + combination problem __c++

Source: Internet
Author: User

To get a combination, such as C (16,3), we can find out how many of these are in the formula, but how do we represent these numbers?

Code:

#include <iostream>
#include <stdio.h>
#define MAXN
using namespace std;
int A[MAXN];
int count=0;
void Assem (int m,int k)
{
    int i,j;
    for (i=m;i>=k;i--)
    {
        a[k]=i;
        if (k>1)
            Assem (i-1,k-1);
        else
        {
             count++;
             /* Show number of permutations for
             (j=a[0];j>0;j--)
             {
                cout<<a[j]<< "  ";
             }
             cout<<endl;*/}}
int main ()
{
    int m,r;
    cout<< "m=";
    cin>>m;
    cout<< "r=";
    cin>>r;
    A[0]=r;
    Assem (m,r);
    cout<<count<<endl;
    return 0;
}
For example, we ask C (16,3), we should be first from 16 number of 1 number "into C (16,1)", then from the remaining 15 numbers to take a number "into C (15,1)", and finally from the remaining 14 number of a number "into C (14,1)" was completed. So this process is easy to implement in a recursive way.

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.