hdu 1568 Fibonacci

水题,注意一点就是前20位不能用公式,后面才可以用……

#include <iostream>
#include <cmath>
using namespace std;
int
ans[22]={0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765};
int
fun(long n)
{

    double
a;
    int
b;
    a=-0.5*log10(5.0)+n*log10((sqrt(5.0)+1)/2);
    b=pow(10.0,a-(int)a+3);
    return
b;
}

int
main()
{

    int
n,sum,num,t=0;
    while
(scanf("%d",&n)!=EOF)
    {

        if
(n<=20)
            cout<<ans[n]<<endl;
        else
cout<<fun(n)<<endl;
    }

    return
0;
}

你可能感兴趣的:(fibonacci)