组合算法的实现,递归,C语言
2015-09-23 14:35
225 查看
组合算法的实现,递归:
#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;
}
#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;
}
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- Lua和C语言的交互详解
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#中的递归APS和CPS模式详解
- C#冒泡法排序算法实例分析
- WinForm实现按名称递归查找控件的方法
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- C#中的尾递归与Continuation详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- 使用C语言判断英文字符大小写的方法