题目大意:输入一个整数t表示测试用例数。接着输入n,输出对应的斐波那契数fib[n]。(一开始,我看成了输入n,然后输出前n个斐波那契数。。。蛋碎了一地)
解题思路:对于用大数来解决的斐波那契数列的相关题目。我们都可以先根据范围创建好数组。然后在需要用的时候,直接在
数组里面取需要用的数即可
代码如下:
package com.njupt.bigInteger;
import java.math.BigInteger;
import java.util.Scanner;
public class HDU_1715_2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BigInteger fib[] = new BigInteger[1001];
fib[1] = new BigInteger("1");
fib[2] = new BigInteger("1");
/**
* 现根据范围创建好斐波那契数列
*/
for (int i = 3; i <= 1000; ++i) {
fib[i] = fib[i - 1].add(fib[i - 2]);
}
while (scanner.hasNextInt()) {
int t = scanner.nextInt();
while (t > 0) {
int i = scanner.nextInt();
System.out.println(fib[i]);
t--;
}
}
}
}