24h購物| | PChome| 登入
2012-03-24 19:38:07| 人氣1,253| 回應0 | 上一篇 | 下一篇

[UVA][JAVA] 10579 - Fibonacci Numbers

推薦 0 收藏 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]);
}
}
}



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

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文