24h購物| | PChome| 登入
2012-03-26 21:27:22| 人氣1,453| 回應0 | 上一篇 | 下一篇

[UVA][JAVA] 10183 - How Many Fibs?

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

Problem B: How many Fibs?

Recall the definition of the Fibonacci numbers:

f1 := 1
f2 := 2
fn := fn-1 + fn-2     (n>=3)
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].

Input Specification

The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a=b=0. Otherwise, a<=b<=10100. The numbers a and b are given with no superfluous leading zeros.

Output Specification

For each test case output on a single line the number of Fibonacci numbers fi with a<=fi<=b.

Sample Input

10 100
1234567890 9876543210
0 0

Sample Output

5
4


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

public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String x, y;
BigInteger[] F = new BigInteger[500];
F[1] = BigInteger.valueOf(1);
F[2] = BigInteger.valueOf(2);
for(int i = 3; i < 500; i++)
F[i] = F[i-1].add(F[i-2]);
while(keyboard.hasNext()) {
x = keyboard.next();
y = keyboard.next();
if(x.equals("0") && y.equals("0"))
break;
BigInteger a, b;
a = new BigInteger(x);
b = new BigInteger(y);
int ans = 0;
for(int i = 1; i < 500; i++) {
if(F[i].compareTo(a) >= 0 && F[i].compareTo(b) <= 0)
ans++;
}
System.out.println(ans);
}
}
}
 

台長: Morris
人氣(1,453) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][JAVA] 10023 - Square root
此分類上一篇:[UVA] 10025 - The ? 1 ? 2 ? ... ? n = k problem

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