题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568

思路:用取对数的方法得到小数部分,再取前四位

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
    int fib[25];
    fib[0]=0;
    fib[1]=1;
    for(int i=2;i<=22;i++)
    fib[i]=fib[i-1]+fib[i-2];
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n<21)printf("%d\n",fib[n]);
        else
        {
            double temp=-0.5*log(5.0)/log(10.0)+((double)n)*log((sqrt(5.0)+1.0)/2.0)/log(10.0);
            temp-=floor(temp);
            temp=pow(10.0,temp);
            while(temp<1000)
            temp*=10;
            printf("%d\n",(int)temp);
        }
    }
}