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. 
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) =

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

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

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

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

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

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

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

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<queue>
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");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值