杭电acm 2070

杭电acm 2070 (递归超时)

Fibbonacci Number
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 39598 Accepted Submission(s): 17737

Problem Description
Your objective for this question is to develop a program which will generate a fibbonacci number. The fibbonacci function is defined as such:

f(0) = 0
f(1) = 1
f(n) = f(n-1) + f(n-2)

Your program should be able to handle values of n in the range 0 to 50.
看到Fibbonacci ,直接去递归了,然后就超时了。。。
与递归比起来,存在数组中,(线性时间),然后直接调用快很多

#include<iostream>
using namespace std;
int main()
{
	int n;
	while(cin>>n&&n!=-1)
	{
	__int64 a[51];
	a[0] = 0;
	a[1] = 1;
	for(int i=2;i<51;i++)
	{
		a[i] = a[i-1]+a[i-2];
	} 
	cout<<a[n]<<endl;
	}
	return 0;
} 
原文地址:https://www.cnblogs.com/serendipity-my/p/12607670.html