HDU 1715 大菲波数 (java大数)

题面

Problem Description
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

代码实现

import java.math.BigInteger;
import java.util.Scanner;

public class hdu1715 {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigInteger fib[] = new BigInteger[1001];
        fib[1] = BigInteger.ONE;
        fib[2] = new BigInteger("1");
        for (int i=3;i<=1000;i++) {
            fib[i] = fib[i-1].add(fib[i-2]);
        }
        while (cin.hasNext()) {
            int t = cin.nextInt();
            while (t>0) {
                int i = cin.nextInt();
                System.out.println(fib[i]);
                t--;
            }
        }
    }
}
posted @ 2020-07-03 22:21  Luglucky  阅读(108)  评论(0编辑  收藏  举报