想吃冰卻又敏感保護不足?女大生禁不起誘惑!按了..正妹的東京自助旅行不捨學生找「槌」 高中...
2012-03-26 21:27:22 人氣(271) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

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

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

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

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

(有*為必填)
詳全文