Hat's Fibonacci

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.

纪念构建大数模板成功!!!!!(大一上学期写的,所以代码有点糙,献丑了0.0)

代码:
#include
using namespace std;
vectorv;
string big_num(string nl,string ml)
{
    //cout<<nl<<" "<<ml<<endl;
    string str="";
    int t,len,lon,j,x=1,l,m[2050],n[2050];
    memset(m,0,sizeof m);
    memset(n,0,sizeof n);
    j=0;
    len=nl.size();
    lon=ml.size();
    for(int i=len-1;i>=0;i--)
    {
        n[i]=nl[j++]-'0';
        //cout<<n[i]<<endl;
    }
    j=0;
    for(int i=lon-1;i>=0;i--)
    {
        m[i]=ml[j++]-'0';
    }
    if(len
    l=lon;
    else
    l=len;
    for(int j=0;j<=l-1;j++)
    {
        n[j]+=m[j];
        if(n[j]>=10)
        {
            n[j+1]=n[j+1]+1;
            n[j]=n[j]-10;
        }
    }
    if(n[l]==0)
    {
        //cout<<n[0]<<endl;
        for(int i=l-1;i>=0;i--)
        {
            str+=(n[i]+'0');
            //cout<<str<<endl;
            //cout<<n[i];
        }
        return str;
    }
    else
    {
        for(int i=l;i>=0;i--)
        {
            str+=(n[i]+'0');
            //cout<<str<<endl;
        }
        //cout<<n<<endl;
        return str;
    }
}
void solve()
{
    v.push_back("1");
    v.push_back("1");
    v.push_back("1");
    v.push_back("1");
    v.push_back("1");
    for(int i=5;;i++)
    {
        //cout<<v[i-1]<<" "<<v[i-2]<<" "<<v[i-3]<<" "<<v[i-4]<<endl;
        v.push_back(big_num(big_num(v[i-1],v[i-2]),big_num(v[i-3],v[i-4])));
       // cout<<"前四项为:";
        //cout<<v[i-1]<<" "<<v[i-2]<<" "<<v[i-3]<<" "<<v[i-4]<<endl;
        //cout<<"和为:";
        //cout<<v[i]<<endl;
        if(v[i].size()>2006)
            return;
    }
}
int main()
{
    //freopen("in.txt", "r", stdin);
    solve();
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        //cout<<"前四项为:";
        //cout<<v[n-1]<<" "<<v[n-2]<<" "<<v[n-3]<<" "<<v[n-4]<<endl;
        //cout<<"和为:";
        cout<<v[n]<<endl;
    }
    return 0;
}

posted @ 2016-05-22 22:28  勿忘初心0924  阅读(106)  评论(0编辑  收藏  举报