栈消递归的几个例子

用堆栈的方式求组合数

#include <stdio.h>
#include<stdlib.h>

typedef struct
{
	int *elem;
	int max_size;//栈的容量
	int top;//栈顶指针
}SqStack;

void main()
{
	void Creat_Stack(SqStack &S,int n);
	void pop(SqStack &S,int &e);
	void push(SqStack &S,int e);
	int n,m,c;
	printf("请输入n,m的值:");
	scanf("%d %d",&n,&m);
	SqStack S;
	Creat_Stack(S,m);
	while(m>=0)push(S,m--);
	while(!(S.top==-1))
	{
		pop(S,m);
		if(m==0)c=1;
		else c=c*(n-m+1)/m;
	}
	printf("%2d和%2d的组合数结果为:%d\n",n,m,c);
}

void Creat_Stack(SqStack &S,int n)
{//建深度为n的栈
	S.max_size=n+1;
	if(!(S.elem=(int *)malloc(sizeof(int)*S.max_size)))exit(-1);
	S.top=-1;
}
void pop(SqStack &S,int &e)
{//出栈函数
	if(S.top==-1)printf("ERROR!栈空!\n");
	e=S.elem[S.top--];
}
void push(SqStack &S,int e)
{//入栈函数
	if(S.top>=S.max_size-1)printf("ERROR!栈满!\n");
	S.elem[++S.top]=e;
}

/**********************************************************************/

用堆栈的方式求两个数的最大公约数

#include <stdio.h>
#include<stdlib.h>

typedef struct
{
	int *elem1;
	int *elem2;
	int max_size;//栈的容量
	int top;//栈顶指针
}SqStack;

void main()
{
	void Creat_Stack(SqStack &S,int n);
	void get_top(SqStack &S,int &a,int &b);
	void push(SqStack &S,int a,int b);
	SqStack S;
	int a,b,a0,b0;
	Creat_Stack(S,5);//栈的容量由辗转相除的次数决定
	printf("请输入a和b的值:");
	scanf("%d%d",&a,&b);
	a0=a;b0=b;
	push(S,a,b);
	while(1)
	{
		get_top(S,a,b);
		if(b!=0)//当b不为0时,使b和a%b入栈
			push(S,b,a%b);
		else break;
	}
	get_top(S,a,b);//当b为0时,取栈顶元素
	printf("%2d和%2d的最大公约数为:%d\n",a0,b0,a);
}

void Creat_Stack(SqStack &S,int n)
{//建深度为n的栈
	S.max_size=n;
	if(!(S.elem1=(int *)malloc(sizeof(int)*S.max_size)))exit(-1);
	if(!(S.elem2=(int *)malloc(sizeof(int)*S.max_size)))exit(-1);
	S.top=-1;
}
void get_top(SqStack &S,int &a,int &b)
{//取栈顶元素函数
	if(S.top==-1)printf("ERROR!栈空!\n");
	a=S.elem1[S.top];
	b=S.elem2[S.top];
}
void push(SqStack &S,int a,int b)
{//入栈函数
	if(S.top>=S.max_size-1)printf("ERROR!栈满!\n");
	++S.top;
	S.elem1[S.top]=a;
	S.elem2[S.top]=b;
}

/**********************************************************************/

用堆栈的方式消去递归函数:在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>

typedef struct
{
	int m;
	int n;
}ElemTp;

typedef struct
{
	ElemTp *elem;//栈内元素
	int max_size;//栈的最大容量
	int top;//栈顶指针
}SqStack;

void main()
{
	void Creat_Stack(SqStack &s,int n);
	void pop(SqStack &s,ElemTp &e);
	void push(SqStack &s,ElemTp e);//一些基本操作函数的说明
	int m,n,y;
	printf("请输入m,n的值:");
	scanf("%d%d",&m,&n);
	ElemTp e={m,n};
	SqStack s;
	Creat_Stack(s,m);
	while(e.m>=0)
	{
		push(s,e);
		e.m=e.m-1;e.n=2*e.n;//将m,n的值依次入栈
	}
	while(!(s.top==-1))
	{
		pop(s,e);//将m,n的值依次出栈进行计算
		if(e.m==0&&e.n>=0)
			y=0;
		else
			y=y+e.n;
	}
	printf("%d\n",y);
}

void Creat_Stack(SqStack &s,int n)
{//建深度为n的栈
	s.max_size=n+1;
	if(!(s.elem=(ElemTp *)malloc(sizeof(ElemTp)*s.max_size)))exit(-1);
	s.top=-1;
}
void pop(SqStack &s,ElemTp &e)
{//出栈函数
	if(s.top==-1)printf("ERROR!栈空!\n");
	e=s.elem[s.top--];
}
void push(SqStack &s,ElemTp e)
{//入栈函数
	if(s.top>=s.max_size-1)printf("ERROR!栈满!\n");
	s.elem[++s.top]=e;
}

/**********************************************************************/

用数组或递归消去递归函数:在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
void main()
{//用数组消去递归
	int n;
	int *p;
	printf("请输入n的值:");
	scanf("%d",&n);
	p=(int *)malloc(sizeof(int)*(n+1));
	for(int i=0;i<=n;i++)
	{
		if(i==0)p[i]=1;
		else p[i]=i*p[i/2];
	}
	printf("函数的结果为:%d\n",p[n]);
}
#include<stdio.h>
#include<stdlib.h>

typedef struct
{//栈的结构体
	int *elem;//栈内元素
	int max_size;//栈的最大容量
	int top;//栈顶指针
}SqStack;

void main()
{//用栈消去递归
	void Creat_Stack(SqStack &s,int n);
	void pop(SqStack &s,int &e);
	void push(SqStack &s,int e);//一些栈的基本操作函数的说明
	int n,e,y;//e用于出栈、出栈元素;y用于返回函数结果
	SqStack s;
	printf("请输入n的值:");
	scanf("%d",&n);
	Creat_Stack(s,n);//建栈
	while(n>0)
	{
		push(s,n);
		n=n/2;
	}//将一系列的自变量的值入栈
	push(s,0);//单独将0入栈
	while(s.top!=-1)
	{
		pop(s,e);//元素出栈并进行计算
		if(e==0)y=1;
		else y=y*e;
	}
	printf("函数的结果为:%d\n",y);
}

void Creat_Stack(SqStack &s,int n)
{//建深度为n的栈
	s.max_size=n+1;
	if(!(s.elem=(int *)malloc(sizeof(int)*s.max_size)))exit(-1);
	s.top=-1;
}

void pop(SqStack &s,int &e)
{//出栈函数
	if(s.top==-1)printf("ERROR!栈空!\n");
	e=s.elem[s.top--];
}

void push(SqStack &s,int e)
{//入栈函数
	if(s.top>=s.max_size-1)printf("ERROR!栈满!\n");
	s.elem[++s.top]=e;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值