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.