一个非递归问题

ttlyfast 2005-09-08 05:48:25
议题提交:老师留的题如下:

f(m,n)= n+1 (m=0);时

f(m,n)=f(m-1,1) (n=0);时

f(m,n)=f(m-1,f(m,n-1)); (n!=0)时

要求用非递归算法编写程序求出当给定一组m,n的值时
fx的值


以下是我的答案:

问题: 我的程序已经调试通过
但是m和n 只能很小才行 请问 有没有办法改进?

方法1_用链栈实现非递归

#include <stdio.h>
#include <malloc.h>



typedef struct node
{int data;
struct node *next;
}stacknode,*linkstack;



linkstack top;
stacknode *g;
int m,n,fx,w;



linkstack init_linkstack()
{return NULL;}



int empty_linkstack(linkstack top)
{if(top==NULL) return 1;
else return 0;
}



linkstack push_linkstack(linkstack top,int x)
{stacknode *s;
s=malloc(sizeof(stacknode));
s->data=x; s->next=top; top=s;



return top;
}



linkstack pop_linkstack(linkstack top,int *x)
{stacknode *p;
if(top==NULL) return NULL;
else{*x=top->data; p=top; top=top->next;
free(p); return top;
}
}







pushf()
{
while(m)
{if(n==0)
{m--;n=1;}
else
{top=push_linkstack(top,m); /*这里如果只写push_linkstack(top,m);程序出错是为甚磨?*/



n--;
}



}
fx=n+1;
return;
}




popf()
{
top=pop_linkstack(top,&m); /*这里如果只写push_linkstack(top,m);程序出错是为甚磨?*/
m--;
n=fx;
printf("\n%d",m);
return;
}




main()
{



scanf("%d,%d",&m,&n);







do
{pushf();



if(top==NULL) break;
popf();
}while(1);
printf("\n %d,%d,%d",fx,m,n);
getch();

}

方法2_用数组标号模拟入栈出栈实现非递归

#include <stdio.h>
#include <malloc.h>


int con[10];
int m;
unsigned long fx;
unsigned long n;
pushf()
{
while(m)
{if(n==0)
{m--;n=1;}
else
{
con[m]++;
n--;
}




}
fx=n+1;
return;
}





popf()
{
int w,i;

for(i=1,w=0;w==0;i++)
if(con[i]!=0)
{m=i;
con[i]--;
w=1;
}




m--;
n=fx;

return;
}





main()
{
int j,k,mc;


scanf("%d,%d",&m,&n);


mc=m;





do
{pushf();

for(j=mc,k=0;k==0&&j>0;j--)
if(con[j]!=0) k=1;

if(k==0) break;


popf();
}while(1);
printf("\n\n fx=%lu",fx);
printf("\n\n n=%lu",n);
getch();

}



...全文
197 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
关于优化的问题
我自己回去 再看看 #:-D
xiaocai0001 2005-09-08
  • 打赏
  • 举报
回复
http://www.csdn.net/help/over.asp
看看这个
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
学生刚刚注册
不知道 怎样给分啊?
xiaocai0001 2005-09-08
  • 打赏
  • 举报
回复
终于给计算出来了
f(4,2)
= f(3,f(4,1))
= f(3, 65533)
= 2^(65533+3) - 3
= 2^(2^16) -3
=(共19728位)

?????
太长了,论坛不让一次发出来!!

郁闷!
xiaocai0001 2005-09-08
  • 打赏
  • 举报
回复
不用优化了~~
优化了也是白做
m只能取0,1,2,3,再加上4,n<=1,再多就溢出了,long double也存不下,那还有什么算头的啊~

取0,1,2,3上面都有计算公式了。不用优化了
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
我哪个烂程序 外部变量多
程序臃肿 有哪位高人能优化一下啊!!
xiaocai0001 2005-09-08
  • 打赏
  • 举报
回复
对不起,对不起
2^65536是多少,算到现在也没算对,真丢人啊~~
应该是19783位数据
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
xiaocai0001(萧筱雨) 老师真是太厉害了
学生 太佩服您的 数学功力了
xiaocai0001 2005-09-08
  • 打赏
  • 举报
回复
计算错了
f(4,1) =65533
f(4,2) = 5822087777506072339445587895905719156733
f(4,3)
= f(3,f(4,2))
= f(3,5822087777506072339445587895905719156733)
= 2^5822087777506072339445587895905719156736 - 3
= ?????
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
高兴 啊
终于有人用计算机算出结果了!!
我那个烂计算机怎磨都算不出m=4,n=2
以前总是以为我的程序有错 所以算不出
现在看来 也许程序对了那
多谢 xiaocai0001(萧筱雨) 老师啊

不过我哪个烂程序 外部变量多
程序臃肿 有哪位高人能优化一下啊!!
xiaocai0001 2005-09-08
  • 打赏
  • 举报
回复
第二个方法也是溢出
f(4,1) = 65533
f(4,2) = f(3,f(4,1)) = f(3, 65533) = 2^(65533+3) - 3 = 2^65536 - 3 这个数字是多大啊?
你有多大的数据类型才容纳啊?
2^65536 - 3 = 1644175555012144678891175791811438313469

所以m,n大数会出错的
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
第2个算法中
int con[10];
定义了 可以计算的m 的范围 为 1-10
(太大担心时间太长)
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
特别针对第一个方法的算法 可能溢出的 问题
我又做了第2个算法 应该不会溢出 (我的愚见)
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
我的 基础知识 忘的是够快了

谢谢
snowbirdfly(胡晋)老师 的热心关注

谢谢
xiaocai0001(萧筱雨)老师 用数学方法探索

能用数学解决最好最快了
xiaocai0001 2005-09-08
  • 打赏
  • 举报
回复
不好意思,下面这个写错了
f(3,n) = 5 * 2^n + 3*n*(n+1)/2

应该是
f(3,n) = 2^(n+3) - 3

还有一个问题,这个计算数据好像很大啊
计算到m=4,n=2时已经溢出了啊
snowbirdfly 2005-09-08
  • 打赏
  • 举报
回复
是要问top=pop_linkstack(top,&m); /*这里如果只写pop_linkstack(top,m);程序出错是为甚磨?*/
上面大哥已经解释了~~~
因为要看你调用的函数原形了:
linkstack pop_linkstack(linkstack top,int *x)//int *x不是int x所以要保持调用函数的形式一致~~m为int类型
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
谢谢 zhouhuahai(道号"虚无") 老师
ttlyfast 2005-09-08
  • 打赏
  • 举报
回复
是不是我改的top 并不是外部变量的top
而是子程序 生成的 另一个地址的top
与原来的(外部变量)的top 在内存中不是一个地址?
(如果反汇编调试的话 也许会发现)
snowbirdfly 2005-09-08
  • 打赏
  • 举报
回复
是啊~~~
处理效率是相对低~~
但是结构比较简单~~
bugebear3 2005-09-08
  • 打赏
  • 举报
回复
好象在程序里采用递归方法,效率会很低.
加载更多回复(9)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧