HDU 1250 Hat's Fibonacci

来源:互联网 发布:淘宝店推广引流的技巧 编辑:程序博客网 时间:2024/06/06 11:47

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

题目大意:

数组F中,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]

#include <iostream>#include<cstring>#include<cstdio>using namespace std;int a[7500][600];int main(){    int b,c,d,e,f,j,g;    memset(a,0,sizeof(a));    a[0][0]=1;a[1][0]=1;a[2][0]=1;a[3][0]=1;    f=0;g=0;    for(b=4;b<7500;b++)    {        for(j=0;j<=g;j++)        {            a[b][j]=a[b-1][j]+a[b-2][j]+a[b-3][j]+a[b-4][j];        }        for(j=0;j<=g;j++)      //a数组中数值>=10000时进位        {            a[b][j+1]+=a[b][j]/10000;            a[b][j]%=10000;        }        if(a[b][g])        {            g++;        }    }    while(cin>>e)    {        for(f=g;f>=0;f--)     //判断直到数组a中数不为0时输出        {            if(a[e-1][f]!=0)                break;        }        cout<<a[e-1][f];        f--;        for(;f>=0;f--)        {            printf("%04d",a[e-1][f]);        }        cout<<endl;    }    return 0;}