zoukankan      html  css  js  c++  java
  • Fibonacci(...刷的前几道题没有记博客的习惯,吃了大亏)

    Fibonacci

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4231 Accepted Submission(s): 1908
     
    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
    Happy 2007
     
    Recommend
    8600
     
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        //freopen("in.txt","r",stdin);
        int f[21];
        f[0]=0;
        f[1]=1;
        for(int i=2;i<=20;i++)
            f[i]=f[i-1]+f[i-2];
        int n;
        while(~scanf("%d",&n))
        {
            if(n<=20)
            {
                printf("%d
    ",f[n]);
                continue;
            }
            else
            {
                //double ans=log10((1/pow(5,0.5))*(pow((1.0+(pow(5,0.5)))/2.0,n)-pow((1.0-(pow(5,0.5)))/2.0,n)));
                double ans=-0.5 * log(5.0) / log(10.0) + ((double)n) * log((sqrt(5.0)+1.0)/2.0) / log(10.0);
                ans-=floor(ans);
                ans=pow(10,ans);
                while(ans<1000)
                    ans*=10;
                printf("%d
    ",(int) ans);
            }
        }
        //printf("%d
    ",a);
    }
  • 相关阅读:
    Go的几种函数式编程范例
    换零钱和快速幂
    随笔不是博客
    leetcode-51
    leetcode-50
    拨号器
    简易计算器的实现
    python入门:1-100所有数的和
    python入门:输出1-10以内除去7的所有数(简)
    python入门:输出1-10以内除去7的所有数(自写)
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6384791.html
Copyright © 2011-2022 走看看