大斐波那契

Fibonacci数列,定义如下: 
f(1)=f(2)=1 
f(n)=f(n-1)+f(n-2) n>=3。 
计算第n项Fibonacci数值。 
Input
输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。 
Output
输出为N行,每行为对应的f(Pi)。 
Sample Input
5
1
2
3
4
5
Sample Output
1
1
2
3
5
发现一种方法在做大数的时候不会超时。
我们知道在做大数的时候会用到两层for循环,所以超时的可能性大大增加,所以我们要做的就是简化代码
我找到一种方法,第一个for循环控制第几个数,第二个数控大数的几位数。我主要下手的地方就是第二个for
循环,我每用一个空间就开辟一个空间,不用就不开辟。
int febonaci(int x)
{
    int a[1009][400];
    int l=1,s=0;                   //l就是大数的长度,s就是进位数
    memset(a,0,sizeof(a));
    a[1][0]=1;
    a[2][0]=1;
    for(int i=3;i<=x;i++)
    {
        for(int j=0;j<l;j++)  //刚开始我就开辟一个空间存数
        {
            a[i][j]=a[i-1][j]+a[i-2][j]+s;
            s=a[i][j]/10;              //当s不为0的时候就说明这个空间存不下了
            a[i][j]%=10;
        }
        while(s)                        //再用一个while循环来判断空间增加多少。这样就不会超
        {                                  //了。
            a[i][l++]=s%10;
            s=s/10;
        }
    }
   for(int i=l-1;i>=0;i--)
    printf("%d",a[x][i]);
   printf("\n");
}
int main()
{
    int t;
    scanf("%d",&t);
  while(t--)
  {
      int n;
      scanf("%d",&n);
      fabonaci(n);
  }
}
这种做法是从另一个博客看到的大数阶乘,我改了一下把它用到大斐波那契上去了。
 
 
 
 
 
 
有一个类似大斐波那契的题:
  
  
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>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int a[10050][600];
void feibonaci(int x)
{
    int l=0;
    memset(a,0,sizeof(a));
    a[1][0]=1;
    a[2][0]=1;
    a[3][0]=1;
    a[4][0]=1;
    for(int i=5;i<=x;i++)
    {
        for(int j=0;j<=l;j++)
        {
            a[i][j]+=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j];
            if(a[i][j]>9999)
            {
                a[i][j+1]+=a[i][j]/10000;
                a[i][j]=a[i][j]%10000;
                if(j==l)
                    l++;
            }
        }
    }
    if(l==0)
        printf("%d",a[x][l]);//第一位要单独输出,因此要做出特判
    else
    {
        printf("%d",a[x][l]);   //第一位可能会存在不满四位的数,所以要做的就是将第一位单独输出。
        for(int i=l-1;i>=0;i--)
            printf("%04d",a[x][i]);  //可能会有0的存在所以要按照四位输出的模式
    }
    printf("\n");
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        feibonaci(n);
    }
}
这种方式去解决上面哪一题就不会怕太坑的数据了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值