最省納智捷1年新車83萬起來自星星的你也會打廣告?正妹的東京自助旅行黃曉明、林志玲攜手代言...
2012-03-28 16:34:22 人氣(806) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA][JAVA] 495 - Fibonacci Freeze

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());
}
}
}

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

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

(有*為必填)
詳全文