C语言 回溯---转非递归---- 输出集合{1,2,...n}的幂集

大清早,打开电脑,做点什么呢?还是忍不住到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();
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言编写的程序,可以打印输出n元(n=1,2,3,4,5,6)集合幂集: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> void print_set(int *set, int n) { printf("{"); for (int i = 0; i < n; i++) { printf("%d", set[i]); if (i < n - 1) { printf(", "); } } printf("}"); } void print_power_set(int **power_set, int n, int num_sets) { for (int i = 0; i < num_sets; i++) { print_set(power_set[i], n); printf("\n"); } } int main() { int n = 6; int num_sets = pow(2, n); int **power_set = (int **)malloc(num_sets * sizeof(int *)); for (int i = 0; i < num_sets; i++) { power_set[i] = (int *)malloc(n * sizeof(int)); } for (int i = 0; i < num_sets; i++) { for (int j = 0; j < n; j++) { if (i & (1 << j)) { power_set[i][j] = j + 1; } else { power_set[i][j] = 0; } } } print_power_set(power_set, n, num_sets); for (int i = 0; i < num_sets; i++) { free(power_set[i]); } free(power_set); return 0; } ``` 该程序使用了位运算的思想来生成幂集,其中: - 变量n表示集合的元素个数; - 变量num_sets表示幂集的元素个数,即2的n次方; - 变量power_set是一个二维数组,用于存储幂集中的所有子集; - 函数print_set用于打印输出一个集合; - 函数print_power_set用于打印输出幂集中的所有子集。 程序的基本思路是: 1. 首先申请内存空间,用于存储幂集中的所有子集; 2. 然后使用位运算的思想,生成幂集中的所有子集; 3. 最后打印输出幂集中的所有子集,并释放内存空间。 希望这个程序能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值