注册 登录
编程论坛 C++教室

请帮忙指出下面的问题并改正!

雪色朝阳 发布于 2010-03-20 20:08, 647 次点击
编写函数fun(),它的功能是求Fibonacci中小于t的最大的一个数,结果由函数返回。Fibonacci数列如下:
F(0)=0,F(1)=1
F(n)=F(n-1)+F(n-2)

请问以下程序有什么问题?

#include    <stdlib.h>
#include    <conio.h>
#include    <math.h>
#include    <stdio.h>
int  fun(int t)
{
int c;
if(t==1)
  return 2;
else c=fun(t-1)+fun(t-2);
return c;
}
void main()
{
 int  n;
 system("CLS");  
 n=1000;
 printf("n=%d,  f=%d\n",n, fun(n));
}
5 回复
#2
cnfarer2010-03-20 21:47
#include    <stdlib.h>
#include    <conio.h>
#include    <math.h>
#include    <stdio.h>
int  fun(int t)
{
if(t==1)
  return 1;
else if (t==0)
  return 0;

else
  return (fun(t-1)+fun(t-2));
}
void main()
{
int  n;
system("CLS");
n=10;        /*这个数字不能太大*/
printf("n=%d,  f=%d\n",n, fun(n));
}
#3
胡马平2010-03-21 07:40
楼上已解决!!!
#4
yangriver2010-03-21 13:28
刚看了一个帖子,main函数不能返回void哦
#5
雪色朝阳2010-03-21 14:09
回复 2楼 cnfarer
为什么n的值不能太大啊,我试了一下,当取n=100时,执行时没有数字显示,只有光标在闪,这是什么原因啊?
#6
cnfarer2010-03-21 14:24
回复 5楼 雪色朝阳
递归是很耗资源的操作!在不停地使用栈空间,而栈空间是有限的.
即使栈空间足够,也可能存在数值很很大超过变量类型值的范围.
1