题解:这道题首先要知道公式: an= (1/√5) * [((1+√5)/2)^n - ((1-√5)/2)^n] (n=1,2,3.....),公式两边对10求导。
log10(an)=-0.5*log10(5.0)+((double)n)*log((sqrt(5.0)+1.0)/2.0)/log(10.0)+log10(1-((1-√5)/(1+√5))^n) log10(1-((1-√5)/(1+√5))^n)--->0 所以写成log10(an)=-0.5*log10(5.0)+((double)n)*log(f)/log(10.0);
最后就是通过与1000比较求出4位数的操作了;
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; int f[21]={0,1,1}; int main() { int n; for(int i=2;i<21;i++) f[i]=f[i-1]+f[i-2]; while(scanf("%d",&n)!=EOF) { if(n<=20) { printf("%d\n",f[n]); //开始较小直接输出 continue; } else { double t= -0.5*log(5.0)/log(10.0)+((double)n)*log((sqrt(5.0)+1.0)/2.0)/log(10.0); t-=floor(t); //向下取整,保证 t-floor(t)>0 t=pow(10.0,t); while(t<1000) t*=10; printf("%d\n",(int)t); } } return 0; }