| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
找到了费波拉切式的规律,但还超时,该怎么写呢?#include<iostream>
using namespace std;
int main()
{
int L=0,answer=0;
while(cin>>L)
{
int i=0,k=-1;
int f1=2, f2=4,f3,f4;
if(L==1) answer=f1;
else if(L==2)answer=f2;
else
{ int H=(int)(((double)L-0.5)/2.0);
//cout<<"H="<<H<<endl;
for(i=1;i<=H;i++)
{
f3=f1+f2;
f4=f2+f3+k;
f1=f3;
f2=f4;
k=-k;
//cout<<"f1="<<f1<<endl;
//cout<<"f2="<<f2<<endl;
}
if(L%2==1)answer=f1;
else answer=f2;
}
cout<<answer<<endl;
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator