• HDU 1250Hat's Fibonacci


    Hat's Fibonacci

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4730    Accepted Submission(s): 1610

    Problem Description
    A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
    F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
    Your task is to take a number as input, and print that Fibonacci number.
     
    Input
    Each line will contain an integers. Process to end of file.
     
    Output
    For each case, output the result in a line.
     
    Sample Input
    100
     
    Sample Output
    4203968145672990846840663646
    
    
    Note:
    No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
    Output
    For each case, output the result in a line.
     
    题目给出公式
    F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4),然后给出n,要求出F[n],数据比较大,要用到大数运算

    AC code:
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    int f[4][2100];
    int ans[2100];
    
    int main()
    {
        int N;
    
        while(cin>>N)
        {
            if(N<=4)
            {
                cout<<"1"<<endl;
                continue;
            }
            memset(f,0,sizeof f);
            memset(ans,0,sizeof ans);
            f[0][0]=f[1][0]=f[2][0]=f[3][0]=1;
            int len=1;
            for(int i=4;i<N;i++)
            {
                int carry=0;
                for(int j=0;j<len;j++)
                {
                    int temp=carry+f[0][j]+f[1][j]+f[2][j]+f[3][j];
                    ans[j]=temp%10;
                    carry=temp/10;
                }
                while(carry!=0)
                {
                    ans[len++]=carry%10;
                    carry/=10;
                }
                for(int j=0;j<len;j++)
                {
                    f[i%4][j]=ans[j];
                }
            }
            for(int i=len-1;i>=0;i--)
                cout<<ans[i];
            cout<<endl;
        }
    }






  • 相关阅读:
    [Unity3D]脚本中Start()和Awake()的区别
    win7中资源管理器不能显示图片预览的解决方法
    Unity Built-in Shader详解三
    Unity Built-in Shader详解二
    php扩展安装
    ImportError: No module named mysql 报错python引用mysql报错
    运维制度
    ps aux|awk -F'[ ]+' '$3>90{print $2}'|xargs -n1 kill -9
    运维面试必知十五大原理知识点
    resize2fs
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3003039.html
Copyright © 2020-2023  润新知