斐波那契数列hdu1250


Hat's FibonacciCrawling in process...Crawling failedTime Limit:1000MS    Memory Limit:32768KB     64bit IO Format:%I64d & %I64u       

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.


#include <iostream>
#include <stdio.h>
using namespace std;
int a[8000][255]={{0}};
int n,k,ans,i,j,m;
int main()
{
    for( i=1; i<5; i++)a[i][1]=1;
    for( i=5; i<8000; i++)
        for(j=1; j<255; j++)
        {
            a[i][j]+=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j];
            a[i][j+1]+=a[i][j]/100000000;
            a[i][j]=a[i][j]%100000000;//注意这里越大越好,我刚开始对10000操作,都是WA,相应的下面也就应该是%。8d
    }
    while(scanf("%d",&n)!=EOF)
    {
        for(j=254; j>0; j--)
            if(a[n][j]!=0)
            {
                k=j;
                cout<<a[n][j];
                break;

            }
        for( m=k-1; m>0; m--)
            printf("%.8d",a[n][m]);//必须这样处理,否则111100001111,则只会输出111101111
            cout<<endl;


    }
    return 0;
}

你可能感兴趣的:(斐波那契数列,高精度)