C语言 回溯---转非递归---- 输出集合{1,2,...n}的幂集
来源:互联网 发布:js中splice的用法 编辑:程序博客网 时间:2024/05/16 06:05
大清早,打开电脑,做点什么呢?还是忍不住到csdn来看看,这两天继续研究数据结构的递归与非递归。现在把昨晚写完的输出集合{1,2,...n}的幂集的非递归算法实现代码贴出来和大家分享一下吧,如果您有什么意见欢迎发送Email:yijiyong100@163.com 。
递归转非递归的方法大致分为两类:
1、分析原始的堆栈情况,自己设定一个堆栈来实现相关的进栈和出栈操作,比如二叉树的遍历算法从递归转到非递归(可以查看相关数据结构的书籍)。
2、根据递归的调用和输出的形式,找到其中的固有规律,设定循环和逻辑控制语句就可以实现非递归,通常情况下非递归的可控制性和执行时间都会优于递归。
这篇文章是关于输出集合{1,2,...n}的幂集的非递归的实现,至于递归实现大家可以参看:http://blog.csdn.net/yijiyong100/archive/2008/10/17/3093094.aspx
/****************************************/
/*Description: Implementation of none recursion*/
/*Print all the power set of n element*/
/*Email:yijiyong100@163.com*/
/*Author:yi_landry Harbin Normal University Computer Science*/
/*Date:2008-10-18*/
/*Copyright:HNU2008.cop*/
/*Environment:turbo c 2.01 English Version*/
/****************************************/
# include<stdlib.h>
# include<stdio.h>
# define OVERFLOW 0
static int ArraySize = 5;
/***************************************/
/*Judge the loop is over or not*/
/***************************************/
int isover(int * a,int len)
{
int i;
len = ArraySize;
for(i=0;i<len;i++)
if (a[i] == 0) return 0;
return 1;
}
/***************************************/
/*the core function of this program*/
/***************************************/
Powerset(int * a,int len)
{
int count = 0;
int * guide= (int *)malloc((len+1)*sizeof(int));
int i,j,pos=len-1,pos1;
len = ArraySize;
for(i=0;i<len;i++)
guide[i]=0;
guide[len]=1;
while(!isover(guide,len))
{
printf(" { ");
for(j=0;j<len;j++)
{
if(j>=pos)
guide[j]=(guide[j]!=guide[j+1]);/*the core code ,the last zero in the guide (include the element after it),the value*/
/*is the original value exclusive or with the value of the next element*/
if(guide[j]==0)
pos1=j;
else
printf(" %d ",a[j]);
}
printf(" } ");
printf("/n");
pos=pos1;
count++;
}
if (isover(guide,len))
{
count++;
printf(" { } /n");
}
printf("The total element in the powerset is %d /n",count);
}
/***************************************/
/*Just a example function*/
/***************************************/
Example()
{
int i;
int * A = (int *)malloc(ArraySize*sizeof(int));
int * c = A;
for (i = 0;i<ArraySize;i++)
{
* c = i + 1;
c++;
}
Powerset(A,ArraySize);
}
/***************************************/
/*The main part of this program*/
/***************************************/
main()
{
int size;
while(1)
{
printf("Please input the size of the array,we will output the powerset. if you want to exit,you can enter -1 !/n");
scanf("%d",&size);
if (size == -1) break;
ArraySize = size;
Example();
}
}
- C语言 回溯---转非递归---- 输出集合{1,2,...n}的幂集
- C语言 回溯的应用 ---- 输出集合{1,2,...n}的幂集
- N皇后问题的非递归回溯和递归回溯
- 非递归的输出1-N的全排列
- 迷宫问题--非递归回溯 C语言实现
- 迷宫问题--非递归回溯 C语言实现
- n个数的全排列C语言递归 (回溯)back-tracking
- n皇后问题【非递归回溯】
- C语言程序-递归和非递归分别实现求n的阶乘
- 回溯法之求n个集合的幂集
- 非递归求集合的幂集
- 1!+2!+...+N!的递归和非递归实现
- 回溯算法 - 转非递归
- 求{1,2,3}的子集————回溯法(递归与非递归)
- Leetcode Median of two sorted arrays 的非递归O(m+n) C语言解法
- C语言:用递归函数DigitSum(n)实现输入1729,输出sum=1+7+2+9=19的值
- 非递归,按序输出集合的全排列
- 非递归,按序输出集合的全排列
- c4-1.h 串 存储表示
- bo4-01~03.cpp 串 算法
- algo4-01~04.cpp 串 主程序
- 一代名导谢晋叶落归根
- 微软正版验证解决方法
- C语言 回溯---转非递归---- 输出集合{1,2,...n}的幂集
- 开篇
- ExtJS专题-选项面板(1)
- J2EE VS .NET
- 各种软件版本命名诠释
- 环境变量的设置和对path , classpth ,java_home设置作用和目的的讲解
- window.open参数完全手册
- 弹出页面回调调用者页面小技巧
- repeater鼠标悬停变色