HDU 1250 Hat's Fibonacci (recursive, addition of large numbers, string)

Hat's Fibonacci

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

 

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.

 

Problem analysis and effect

Is a variant of Fibonacci, when dealing with large numbers added I used the string bug was stumbling for a long time ... tomorrow a lot to learn string

#include<bits/stdc++.h>

using namespace std;

int n,i;
string bigadd(string a,string b)
{
    int jin=0,i;
    char ai,bi;
    string anss=a;
    int lena=a.size();
    int lenb=b.size();
    int lenmax=max(lena,lenb);
    int p=lena-1;
    int q=lenb-1;
    for(i=lenmax-1;i>=0;i--) 
    { 
        If (p < 0 ) 
        V = ' 0 ' ;
        Leather 
        and = a [p];
        if (q < 0 ) 
        bi = ' 0 ' ;
        Leather 
        bi = b [q]; 
        anss [i] = ((rust ' 0 ' + bi- ' 0 ' + Jin)% 10 ) + ' 0 ' ; 
        Jin = (rust ' 0 '+bi-'0'+jin)/10;
        p--;
        q--;
    }
    if(jin)
    {
        char x=jin+'0';
        anss=x+anss;
    }
    return anss;
}
/*
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        string a="1";
        string b="1";
        string c="1";
        string d="1";
        for(i=5;i<=n;i++)
        {
            TEMP D = String; 
            D = bigadd (bigadd (A, B), bigadd (C, D)); 
            A = B; 
            B = C; 
            C = TEMP; 
        } 
        COUT << << A << B C D << endl <<; 
    } 
 } * / 
 int main () {  
     string a [ 8008 ];     // I before use four string length and seems to be wrong about the 
    a [ . 1 ] = " . 1 " ;   
    a [ 2 ] = " . 1 " ;   
    A [ . 3 ] = " . 1 " ;  
    a[4]= " . 1 ";   
    for(i=5;i<8008;++i)  
           a[i]=bigadd(bigadd(bigadd(a[i-1],a[i-2]),a[i-3]),a[i-4]);  
    while(scanf("%d",&n)!=EOF)
    {
        cout<<a[n]<<endl;
    }
    return 0;       
}

 

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/11330299.html