ZCMU 1041: 二哥的困惑 Ⅳ(大数斐波那契数列)

1041: 二哥的困惑 Ⅳ

Description
A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.

f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)

Your task is to take a number as input, and print that Fibonacci number.

Input

Input contains several cases, every case contains a positive integer number N.

Output
print the Nth Fibonacci number.

Sample Input

100

Sample Output

354224848179261915075

HINT
一个大数斐波那契数列

错误代码1Code(超时了):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int a[1000000];
int b[1000000];
int c[1000000];
void dsf(int n)
{

    //printf("1\n");
    if(n==0)
    {
        printf("0");
        return ;
    }
    if(n==1||n==2)
    {
        printf("1");
        return ;
    }
    int l1=1,l2=1,l3=1;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    n-=2;
    a[0]=1;b[0]=1;
    while(n--)
    {
        //printf("1\n");
        memset(c,0,sizeof(c));
        for(int i=0; i<l2; i++)
        {
            c[i]+=a[i]+b[i];
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
        //printf("1\n");
        if(c[l1]==1)l3++;
        //for(int i=0; i<l3; i++)printf("%d",c[i]);
        for(int i=0; i<l2; i++)
        {
            a[i]=b[i];
        }
        l1=l2;
        for(int i=0; i<l3; i++)
        {
            b[i]=c[i];
        }
        l2=l3;//printf("1\n");
    }
    //printf("%d\n",c[0]);
    for(int i=l3-1; i>=0; i--)printf("%d",c[i]);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        dsf(n);
        printf("\n");
    }
    return 0;
}

错误代码二(超时):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int a[1000000];
int b[1000000];
int c[1000000];
void dsf(int n)
{

    //printf("1\n");
    if(n==0)
    {
        printf("0");
        return ;
    }
    if(n==1||n==2)
    {
        printf("1");
        return ;
    }
    int l1=1,l2=1,l3=1;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    n-=2;
    a[0]=1;b[0]=1;
    while(n--)
    {
        //printf("1\n");
        memset(c,0,sizeof(c));
        for(int i=0; i<l2; i++)
        {
            c[i]+=a[i]+b[i];
            c[i+1]+=c[i]/100000000;
            c[i]%=100000000;
        }
        //printf("1\n");
        if(c[l1]==1)l3++;
        //for(int i=0; i<l3; i++)printf("%d",c[i]);
        for(int i=0; i<l2; i++)
        {
            a[i]=b[i];
        }
        l1=l2;
        for(int i=0; i<l3; i++)
        {
            b[i]=c[i];
        }
        l2=l3;//printf("1\n");
    }
    //printf("%d\n",c[0]);
    for(int i=l3-1; i>=0; i--)printf("%d",c[i]);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        dsf(n);
        printf("\n");
    }
    return 0;
}

AC代码(最后还是打了表):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int i, j, n, a[8000][255];
int main(void)
{
    a[1][1] = 1;
    a[2][1] = 1;
    for (i = 3; i<8000; i++)
        for (j = 1; j<255; j++)
        {
            a[i][j] += a[i - 1][j] + a[i - 2][j];
            a[i][j + 1] += a[i][j] / 100000000;
            a[i][j] = a[i][j] % 100000000;
        }
 
    while (~scanf("%d", &n))
    {
        for (i = 254; i>0; i--)
            if (a[n][i])
                break;
        printf("%d", a[n][i]);
        for (--i; i>0; i--)
            printf("%.8d", a[n][i]);
        printf("\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值