Read topic
what's wrong with my answer,who can tell me
2009-11-22 15:26:54 liuxiaochun
#include<iostream>
using namespace std;
int Fibonacci(int n);
int main()
{
int num;
while(cin>>num)
{
if(num>40||num<2)
break;
else
cout<<"The Fibonacci Number for "<<num<<" is "<<Fibonacci(num)<<endl;
}
return 0;
}
int Fibonacci(int n)
{
if(n==0)
return 0;
else
if(n==1)return 1;
else
return Fibonacci(n-1)+Fibonacci(n-2);
}
Re:what's wrong with my answer,who can tell me
2010-10-11 17:11:50 mrhead
1.your program never compute Fibonacci(0) and Fibonacci(1).
2.the output format of your program does not meet requirement.
3.you use recursive algorithm which might be over time-limits.
Re:what's wrong with my answer,who can tell me
2012-01-20 18:26:05 bnswdnrz
会超时
大家注意输入数字包括40, @:)
2012-09-13 01:15:29 daihu000
Re:what's wrong with my answer,who can tell me
2012-11-22 20:26:44 dengjunjun
# include <stdio.h>
int main()
{
int i,s[41];
s[0]=0;
s[1]=0;
s[2]=1;
s[3]=1;
for(i=4;i<=41;++i)
s[i]=s[i-1]+s[i-2];
int a,b;
while(scanf("%d",&a)!=EOF)
printf ("The Fibonacci number for %d is %d\n",a,(s[a]+s[a-1]));
return 0;
}
Reply
|