大菲波数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19278 Accepted Submission(s): 6450
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.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
BigInteger[] arr = new BigInteger[1001];
arr[1] = new BigInteger("1");
arr[2] = new BigInteger("1");
for (int i = 3; i < 1001; i++) {
arr[i] = arr[i - 1].add(arr[i - 2]);
}
int n = sc.nextInt();
while (n-- > 0) {
int num = sc.nextInt();
System.out.println(arr[num]);
}
}
}
}