NYOJ 461

 

Fibonacci数列(四)

时间限制: 1000 ms | 内存限制: 65535 KB
难度: 4
 
描述
数学神童小明终于把0到100000000的Fibonacci数列(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来。
接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了。所以规定超过4位的只要说出前4位(高4位)就可以了,可是CodeStar自己又记不住。于是他决定编写一个程序来测验小明说的是否正确。
 
输入
输入若干数字n(0 <= n <= 100000000),每个数字一行。读到文件尾结束。
输出
输出f[n]的前4个数字(若不足4个数字,就全部输出)。
样例输入
0

1

2

3

4

5

35

36

37

38

39

40

样例输出
0

1

1

2

3

5

9227

1493

2415

3908

6324

1023


 1 //http://blog.ac521.org/?p=91

 2 #include <stdio.h>

 3 #include <math.h>

 4 int main()

 5 {

 6     int a[21],i,n;

 7     double temp;

 8     for(i=2,a[0]=0,a[1]=1;i<=20;i++)

 9         a[i]=a[i-1]+a[i-2];                                                

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

11     {

12         if(n<=20)  

13                printf("%d\n",a[n]);//必须有,否则不够四位时,用下面的会多输出0,即扩大倍数 

14         else 

15         {

16               temp=n*log((1+sqrt(5.0))/2.0)/log(10.0)-0.5*log(5.0)/log(10.0);

17               temp-=floor(temp);

18               temp=pow(10.0,temp);

19               while(temp<1000)

20                    temp*=10;

21               printf("%d\n",(int)(temp));

22           }                         

23      }

24     return 0;

25 }

26                                                                                                 

27                 

 

你可能感兴趣的:(OJ)