用大数加法实现斐波那契数列

E - Edge Case

 

F[3]=4;

F[4]=7;

F[5]=11;

依次类推,求[3,2000]的斐波那契数

 

 

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
using namespace std;
int n;
int F[10001][2500];
 
int main()
{
    memset(F, 0, sizeof(F));
    F[3][1] = 4, F[3][0] = 1;//F[i][0]表示的是第i个斐波那契数的长度
    F[4][1] = 7, F[4][0] = 1;
    for (int i = 5; i <= 10000; i++)
    {
        int in = 0;//进位
        int j;
        for (j = 1; j <= F[i - 1][0]; j++)
        {
            F[i][j] = F[i - 1][j] + F[i - 2][j] + in;
            in = F[i][j] / 10;
            F[i][j] = F[i][j] % 10;
            F[i][0]++;
        }
        if (in)
        {
            F[i][j] = in;
            F[i][0]++;
        }
    }
    while (cin >> n)
    {
        for (int j = F[n][0]; j >= 1; j--)
            cout << F[n][j];
        cout << endl;
    }
    return 0;
}

 

posted @ 2019-08-06 10:11  知道了呀~  阅读(679)  评论(0编辑  收藏  举报