之前没学过数论写出来的很低级:
#include<iostream>
using namespace std;
int fib(int n){
if(n>1)
return fib(n-1)+fib(n-2);
else
return n;
}
int pan(int a){
while(a>10000){
a=a/10;
}
return a;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
cout<<pan(fib(n))<<endl;
}
return 0;
}
后来看了别人的发现数论真有用!
#include<stdio.h>
#include<math.h>
#define a (sqrt(5.0)+1.0)/2
int main()
{
int n,i,f[21]={0,1};
double ans;
for(i=2;i<21;i++)
f[i]=f[i-1]+f[i-2];
while(scanf("%d",&n)!=EOF)
{
if(n<21)printf("%d\n",f[n]);
else
{
ans=-0.5*(log10(5.0))+n*log10(a);
ans-=(int)ans;
ans=pow(10.0,ans);
while(ans<1000)ans*=10;
printf("%d\n",(int)ans);
}
}
return 0;
}
源代码来自: 点击打开链接