UVA495 Fibonacci Freeze【大数+万进制】

The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...) are defined by the recurrence:

      F0 = 0

      F1 = 1

      Fi = Fi−1 + Fi−2 for all i ≥ 2

  Write a program to calculate the Fibonacci Numbers.

Input

The input to your program would be a sequence of numbers smaller or equal than 5000, each on a separate line, specifying which Fibonacci number to calculate.

Output

Your program should output the Fibonacci number for each input value, one per line.

Sample Input

5

7

11

Sample Output

The Fibonacci number for 5 is 5

The Fibonacci number for 7 is 13

The Fibonacci number for 11 is 89


问题链接UVA495 Fibonacci Freeze

问题简述:(略)

问题分析

  大数计算问题。

程序说明

  用二维数组来表示大数,并且采用万进制,高位在每一行的右边(低位在左边)。

  其他都是套路。

题记:(略)

参考链接:(略)


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

/* UVA495 Fibonacci Freeze */

#include <bits/stdc++.h>

using namespace std;

const int N = 5000;
const int N2 = 300;
const int BASE = 1e4;
int fib[N+ 1][N2 + 1];

void maketable()
{
    memset(fib, 0, sizeof(fib));

    fib[0][0] = 0;
    fib[1][0] = 1;
    for ( int i = 2 ; 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] / BASE;
            fib[i][j] %= BASE;
        }
    }
}

int main()
{
    maketable();

    int n;
    while(~scanf("%d", &n)) {
        int i = N2;
        while ( i >= 1 && !fib[n][i] )
            i --;
        printf("The Fibonacci number for %d is ", n);
        printf("%d", fib[n][i--]);
        while ( i >= 0 )
            printf("%04d", fib[n][i--]);
        printf("\n");
    }

    return 0;
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值