计算超大斐波那契数列的方法

理论上能算无限位,只要有内存~~

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long ll;
int main(){
    int n,i,j,temp=0;
    cin>>n;
    char a[10010]="1",b[10010]="1",c[10010]={0};
    n++;
    for(i=0;i<n-2;i++){
        int len=max(strlen(a),strlen(b));
        for(j=0;j<len;j++){   //模拟乘法
            temp=0;
            if(a[j]>='0'){    //短的数不加
                temp+=a[j]-'0';
            }
            if(b[j]>='0'){
                temp+=b[j]-'0';
            }
            if(temp>=10){      //判断进位
                if(c[j]>='0'){
                    c[j]+=temp-10;
                }
                else{
                    c[j]+=temp-10+'0';
                }
                //c[j+1]-='0';
                c[j+1]=1+'0';
            }
            else{
                if(c[j]>='0'){
                    if(temp==9){   //最坑的地方,若是前位进位了,而且加上的数字是9,那么还要进位!!!
                        c[j]='0';
                        c[j+1]='1';
                    }
                    else{
                        c[j]+=temp;
                    }
                }
                else{
                    c[j]+=temp+'0';
                }
            }
        }
        strcpy(a, b);//把数字倒过去
        strcpy(b, c);
        memset(c, 0, sizeof(c));
    }
    int len=strlen(b);
    for(i=len-1;i>=0;i--){  //因为反着算的,倒着输出
        printf("%c",b[i]);
    }
    printf("\n");
    return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值