hdu 1250 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.
高精度加法,用结构体实现,先打表,节省时间,代码如下:
#include<stdio.h>
struct bignum{
    int s[710];
    int l;
}f[8011];
int m=10000;
bignum operator+(bignum a,bignum b)//a=a+b
{
    int i,d=0;
    if(b.l<a.l)
        b.l=a.l;
    for(i=0;i<b.l;i++)
    {
        b.s[i]+=d+a.s[i];
        d=b.s[i]/m;
        b.s[i]%=m;
    }
    if(d) b.s[b.l]=d,b.l++;
    return b;
}
void print(bignum a)//打印大数据的数组
{
    printf("%d",a.s[a.l-1]);
    for(int i=a.l-2;i>=0;i--)
        printf("%04d",a.s[i]);
    printf("\n");
}
int main()
{
    int i,n;
    f[1].s[0]=f[2].s[0]=f[3].s[0]=f[4].s[0]=1;
    f[1].l=f[2].l=f[3].l=f[4].l=1;
    for(i=5;i<=8000;i++)
        f[i]=f[i-1]+f[i-2]+f[i-3]+f[i-4];
    while(scanf("%d",&n)!=EOF)
    {
        print(f[n]);
    }
    return 0;
}
    

 

 

 

posted @ 2014-04-09 12:24  段少  阅读(167)  评论(0编辑  收藏  举报