想吃冰卻又敏感保護不足?6檔好股票放一年賺5成法人:14檔今年本業虧定了買太多也不行!7500...
2012-03-24 19:38:07 人氣(289) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA][JAVA] 10579 - Fibonacci Numbers

0
收藏
0
推薦


  Problem E: 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 and Output 

Your task is to take numbers as input (one per line), and print the corresponding Fibonacci number.

Sample Input 

3
100

Sample Output 

2
354224848179261915075



import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
int n;
Scanner keyboard = new Scanner(System.in);
BigInteger[] F = new BigInteger[5000];
F[1] = BigInteger.valueOf(1);
F[2] = BigInteger.valueOf(1);
for(n = 3; n < 5000; n++) {
F[n] = F[n-1].add(F[n-2]);
}
while(keyboard.hasNext()) {
n = keyboard.nextInt();
System.out.println(F[n]);
}
}
}



10579Fibonacci Numbers
台長:Morris
人氣(289) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Math] 11038 - How Many O's?
此分類上一篇:[UVA] 12019 - Doom's Day Algorithm

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文