hdu1250 Hat's Fibonacci

Hat’s Fibonacci

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

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
斐波那契数列,大数;
大数相加模板:http://blog.csdn.net/zxy160/article/details/53044156
这里换一种做法;因为感觉比大数模板短,而且有意思;

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int f[10000][260]= {0};//最长2005位,那么一个数组存八位数,开260的空间就够了
int n;
int i,j;
void init()
{
    f[1][0]=1;
    f[2][0]=1;
    f[3][0]=1;
    f[4][0]=1;
    for(i=5; i<10000; i++)
    {
        for(j=0; j<260; j++)
            f[i][j]=f[i-1][j]+f[i-2][j]+f[i-3][j]+f[i-4][j];//对应上面四位数的位置并相加
            //求出现在的数
        for(j=0; j<260; j++)
            if(f[i][j]>100000000)//如果超过8位数,向前进
            {
                f[i][j+1]+=f[i][j]/100000000;
                f[i][j]=f[i][j]%100000000;
            }
    }
}
int main()
{
    init();
    while(~scanf("%d",&n))
    {
        for(i=259; i>=0; i--)//找到高位不为0的那个数
        {
            if(f[n][i]!=0)
                break;
        }
        printf("%d",f[n][i]);//先输出
        for(j=i-1; j>=0; j--)//从=下一位开始
        {
            printf("%08d",f[n][j]);//输出8位,不够左补0
        }
        printf("
");
    }
    return 0;
}
"No regrets."
原文地址:https://www.cnblogs.com/zxy160/p/7215145.html