杭电1568

题意:小于四位的斐波那契数fib(n)直接输出,大于四位的斐波那契数只输出最左边四位,n<100000000。

Analyse:跟1060相同原理。10^x=fib(n)(用通项公式),求出x。令y=3.·#¥%……—*(小数部分是x的小数部分),再输出10^y

View Code
 1 #include<stdio.h>
 2 #include<math.h>
 3 #define C ((sqrt(5)+1)/2)
 4 int fib(int n)
 5 {
 6     return (int)( (pow( (sqrt(5)+1)/2 , n ) - pow( (1 - sqrt(5))/2 , n))/sqrt(5) );
 7 }
 8 int left4(int n)
 9 {
10     double x,y;//10的指数fib(n)==10^x
11     x=n*log10(C)-0.5*log10(5);
12     y=x-floor(x);
13     y+=3;
14     return (int)pow(10,y);
15 }
16 int main()
17 {
18     int n;
19     while(scanf("%d",&n)!=EOF)
20     {
21         if(n<=20)
22             printf("%d\n",fib(n));
23         else
24             printf("%d\n",left4(n));
25     }
26     return 0;
27 }
原文地址:https://www.cnblogs.com/ZShogg/p/2559599.html