HDU 1568 Fibonacci 数学= = 开篇

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

分析:一道数学题

找出斐波那契数列的通项公式,再利用对数的性质就可得到前几位的数

斐波那契通项公式如下:

斐波那契公式

取完对数后(记fn为第n个数)

log10(fn)=-0.5*log10(5.0)+((double)n)*log(f)/log(10.0)+log10(1-((1-√5)/(1+√5))^n)  其中f=(sqrt(5.0)+1.0)/2.0;

最后取对数的小数部分就可得最终结果

代码如下:

 1 #include<cstdio>

 2 #include<cmath>

 3 #include<cstring>

 4 #include<iostream>

 5 #include<algorithm>

 6 using namespace std;

 7 const double f=(sqrt(5.0)+1)/2.0;

 8 int fi[20];

 9 int main()

10 {

11     int n,i;

12     double bit;

13     fi[0]=0;fi[1]=fi[2]=1;

14     for(i=3;i<=20;i++)

15     {

16         fi[i]=fi[i-1]+fi[i-2];

17     }

18     while(scanf("%d",&n)!=EOF)

19     {

20         if(n<=20)

21             printf("%d\n",fi[n]);

22         else{

23             bit=-0.5*log10(5.0)+((double)n)*log(f)/log(10.0);

24             bit=bit-(int)bit;

25             bit=pow(10.0,bit);

26             while(bit<1000)bit*=10;

27             printf("%d\n",(int)bit);

28         }

29     }

30     return 0;

31 }
View Code

 

你可能感兴趣的:(fibonacci)