UVA10579 Fibonacci Numbers【大数】

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)

Input

Your task is to take numbers as input (one per line).

Output

And then print the corresponding Fibonacci number (one per line also).

Sample Input

3

100

Sample Output

2

354224848179261915075

Note: No generated fibonacci number in excess of 1000 digits will be in the test data, i.e. f(20) = 6765 has 4 digits.


问题链接UVA10579 Fibonacci Numbers

问题简述:(略)

问题分析一个简单的大数加法问题。

程序说明

  用数组来模拟大数计算。这里用二维数组来进行模拟计算Fibonacci数列。

  打表,根据输入的n,输出结果。

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* UVA10579 Fibonacci Numbers */

#include <bits/stdc++.h>

using namespace std;

const int N = 5000;
const int N2 = 1000;
char fib[N + 1][N2 + 1];

int main()
{
    memset(fib, 0, sizeof(fib));

    fib[1][0] = fib[2][0] = 1;
    for(int i = 3; i <= N; i++) {
        for(int j = 0; j <= N2; j++)
            fib[i][j] = fib[i - 2][j] + fib[i - 1][j];
        for(int j = 0; j<= N2; j++) {
            fib[i][j + 1] += fib[i][j] / 10;
            fib[i][j] %= 10;
        }
    }

    int n;
    while(~scanf("%d", &n)) {
        int right = N2;
        while(right > 0 && !fib[n][right])
            right--;
        while(right >=0)
            printf("%d", fib[n][right--]);
        printf("\n");
    }

    return 0;
}









猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/80076316
今日推荐