组合算法的实现,递归:
#include "stdafx.h"
#include<iostream>
#define COMBINE_MAXN 1000
int s[COMBINE_MAXN];
int p, q; //C(p,q)
int c[COMBINE_MAXN]; //middle reults stack
int push=0; //middle results pointer
void combineCXX(int *s,int n,int t)
{
//C(n,t)=C(n-1,t)+C(n-1,t-1)
int j;
if ((n > t) && (t>0))
{
combineCXX(s + 1, n - 1, t);
c[push++] = *s;
combineCXX(s + 1, n - 1, t - 1);
push--;
}
else
{
for (j = 0; j <push; j++)
printf("%d", c[j]);
for (j = 0; j < t; j++)
printf("%d", s[j]);
printf("\n");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
for (i = 0; i < COMBINE_MAXN; i++)
s[i] = i;
p = 4; //C(p,q)
q = 4;
combineCXX(s, p, q);
system("pause");
return 0;
}