24h購物| | PChome| 登入
2012-03-28 16:34:22| 人氣2,131| 回應0 | 上一篇 | 下一篇

[UVA][JAVA] 495 - Fibonacci Freeze

推薦 0 收藏 0 轉貼0 訂閱站台

 Fibonacci Freeze 

The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...) are defined by the recurrence:

eqnarray20

Write a program to calculate the Fibonacci Numbers.

Input and Output

The input to your program would be a sequence of numbers smaller or equal than 5000, each on a separate line, specifying which Fibonacci number to calculate.

Your program should output the Fibonacci number for each input value, one per line.

Sample Input

5
7
11

Sample Output

The Fibonacci number for 5 is 5
The Fibonacci number for 7 is 13
The Fibonacci number for 11 is 89



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

public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
BigInteger[] F = new BigInteger[5001];
F[0] = BigInteger.valueOf(0);
F[1] = BigInteger.valueOf(1);
for(int i = 2; i <= 5000; i++) {
F[i] = F[i-1].add(F[i-2]);
}
int n;
while(keyboard.hasNextInt()) {
n = keyboard.nextInt();
System.out.println("The Fibonacci number for " + n + " is " + F[n].toString());
}
}
}

台長: Morris
人氣(2,131) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11005 - Cheapest Base
此分類上一篇:[UVA] 11001 - Necklace

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