Hat's Fibonacci

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. 
InputEach line will contain an integers. Process to end of file. 
OutputFor 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) =

思路:这道题吧,斐波那契大数,应该没什么难度,用二维数组存下当前数的每一位,举例:

a[3][1]指第三个数的第一位,当进位大于十,说明需要再开一个空间存下进位,也就是

a[i][++k]=c;然后就模拟了大数运算。。是不是很简单。,。。

但是这道题并没有说明n的取值范围,只是告诉了最大长度不超过2005位,所以,我采用

滚动数组。不至于浪费内存,或者是爆掉内存。

本人知识有限,,,勿喷。。。(对了,学长说,只要能用G++提交的代码不要用c++提交);

因为,我的代码c++RE,G++就过了,可能与水平有关。。但还是不想承认。。。哈哈、

代码:

#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int N=3000;
int a[4][N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        a[1][1]=1;
        a[2][1]=1;
        a[3][1]=1;
        a[4][1]=1;
        int c=0,k=1;
        for(int i=5; i<=n; i++)
        {
            for(int p=1; p<=k; p++)
            {
                a[0][p]=a[1][p];
                a[1][p]=a[2][p];
                a[2][p]=a[3][p];
                a[3][p]=a[4][p];
            }
            for(int j=1; j<=k; j++)
            {
                int s=a[0][j]+a[1][j]+a[2][j]+a[3][j]+c;
                a[4][j]=s%10;
                c=s/10;
            }
            if(c!=0)
            {
                a[4][++k]=c;
                c=0;
            }
        }
        for(int i=k; i>=1; i--)
            printf("%d",a[4][i]);
        printf("\n");
    }
}


你可能感兴趣的:(ACM竞赛,【含有一定技巧】,【强行模拟,最为致命】,ACM的进程)