zoukankan      html  css  js  c++  java
  • HDU1568

    Fibonacci

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4805    Accepted Submission(s): 2223


    Problem Description
    2007年到来了。经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列
    (f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来。
    接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了。所以规定超过4位的只要说出前4位就可以了,可是CodeStar自己又记不住。于是他决定编写一个程序来测验zouyu说的是否正确。
     
    Input
    输入若干数字n(0 <= n <= 100000000),每个数字一行。读到文件尾。
     
    Output
    输出f[n]的前4个数字(若不足4个数字,就全部输出)。
     
    Sample Input
    0 1 2 3 4 5 35 36 37 38 39 40
     
    Sample Output
    0 1 1 2 3 5 9227 1493 2415 3908 6324 1023
     
    Author
    daringQQ
     
    Source
    代码:
     1 /*
     2 斐波那契数列求和公式:(/5)表示根号5,f[n]=1/(/5) * (((1+(/5))/2)^n-((1-(/5))/2)^n)
     3 然后就是神奇的long10,对两边同时long10的long10(f[n])=long10(1/(/5))+long10(((1+(/5))/2)^n)+long10(1-((1-(/5))/(1+(/5)))^n)
     4 long10(1-((1-(/5))/(1+(/5)))^n)趋于0。只要求出long10(1/(/5))+long10(((1+(/5))/2)^n)就是10的幂次。
     5 所以根据以前博客里的的一道题,10^(整数部分+小数部分),而小数部分在乘几次10取整就是要求的结果。
     6 */
     7 #include<iostream>
     8 #include<cstdio>
     9 #include<cmath>
    10 using namespace std;
    11 int main()
    12 {
    13     int n;
    14     int f[25];
    15     f[0]=0;f[1]=1;
    16     for(int i=2;i<=20;i++)
    17     f[i]=f[i-1]+f[i-2];
    18     while(scanf("%d",&n)!=EOF)
    19     {
    20         if(n<=20)
    21         printf("%d
    ",f[n]);
    22         else
    23         {
    24             double tem=-0.5*log10(5.0)+(double)n*log10((1.0+sqrt(5.0))/2.0);
    25             int ttem=tem;
    26             tem-=ttem;
    27             double ans=pow(10.0,tem);
    28             while(ans<1000)
    29             ans*=10;
    30             ttem=ans;
    31             printf("%d
    ",ttem);
    32         }
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    Django+Bootstrap+Mysql 搭建个人博客(一)
    Django+nginx+uwsgi部署教程(centos7+ubuntu16.4)
    微信小程序入门(六)
    微信小程序入门(五)
    微信小程序入门(四)
    微信小程序入门(三)
    微信小程序入门(二)
    遗传算法常见问题解惑
    记录使用python实现QPSO求解最大值问题时,遇到的问题
    关于JetBrain系列软件的学生授权认证和授权到期(一年)重新申请的问题
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/5947565.html
Copyright © 2011-2022 走看看