求助:ACM的一道题,看着很简单,提交却WA了,求解释
题目:MorethanFibonacciTimeLimit:1000msMemoryLimit:65536kbDescriptionTheNewFibonaccinumb...
题目:
More than Fibonacci
Time Limit: 1000ms
Memory Limit: 65536kb
Description
The New Fibonacci numbers (0, 1, 2, 3, 6, 11, 20, 37, 68, ...) are defined by the recurrence: F(0) = 0; F(1) = 1; F(2) = 2;
F(n) = F(n-1) + F(n-2) + F(n-3) for all n>2
Write a program to calculate the New Fibonacci numbers.
Input
The input is a sequence of integers not more than 36, each on a separate line, specifying which New Fibonacci number to calculate.
Output
Print the New Fibonacci numbers in the format shown in Sample Output.
Sample Input
5
7
11
Sample Output
The New Fibonacci number for 5 is 11
The New Fibonacci number for 7 is 37
The New Fibonacci number for 11 is 423
我的代码:
#include<stdio.h>
main()
{
long F[37];
int i,n;
while(scanf("%d",&n)!=EOF)
{
F[0]=0;F[1]=1;F[2]=2;
for(i=3;i<=n;i++)
{
F[i]=F[i-1]+F[i-2]+F[i-3];
}
printf("The New Fibonacci number for %d is %ld\n",n,F[i-1]);
}
} 展开
More than Fibonacci
Time Limit: 1000ms
Memory Limit: 65536kb
Description
The New Fibonacci numbers (0, 1, 2, 3, 6, 11, 20, 37, 68, ...) are defined by the recurrence: F(0) = 0; F(1) = 1; F(2) = 2;
F(n) = F(n-1) + F(n-2) + F(n-3) for all n>2
Write a program to calculate the New Fibonacci numbers.
Input
The input is a sequence of integers not more than 36, each on a separate line, specifying which New Fibonacci number to calculate.
Output
Print the New Fibonacci numbers in the format shown in Sample Output.
Sample Input
5
7
11
Sample Output
The New Fibonacci number for 5 is 11
The New Fibonacci number for 7 is 37
The New Fibonacci number for 11 is 423
我的代码:
#include<stdio.h>
main()
{
long F[37];
int i,n;
while(scanf("%d",&n)!=EOF)
{
F[0]=0;F[1]=1;F[2]=2;
for(i=3;i<=n;i++)
{
F[i]=F[i-1]+F[i-2]+F[i-3];
}
printf("The New Fibonacci number for %d is %ld\n",n,F[i-1]);
}
} 展开
展开全部
#include<stdio.h>
int main()
{
long F[37];
int i,n;
F[0]=0;F[1]=1;F[2]=2;
for(i=3;i<=36;i++) //拿出来算会更快
F[i]=F[i-1]+F[i-2]+F[i-3];
while(scanf("%d",&n)!=EOF)
{
printf("The New Fibonacci number for %d is %d\n",n,F[n]); //你的是F[n-1],就这里错了。
}
}
int main()
{
long F[37];
int i,n;
F[0]=0;F[1]=1;F[2]=2;
for(i=3;i<=36;i++) //拿出来算会更快
F[i]=F[i-1]+F[i-2]+F[i-3];
while(scanf("%d",&n)!=EOF)
{
printf("The New Fibonacci number for %d is %d\n",n,F[n]); //你的是F[n-1],就这里错了。
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询