2012-03-26 21:27:22Morris
[UVA][JAVA] 10183 - How Many Fibs?
Problem B: How many Fibs?
Recall the definition of the Fibonacci numbers:
f1 := 1Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].
f2 := 2
fn := fn-1 + fn-2 (n>=3)
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);
}
}
}