How Many Fibs?
直接用java大数做,注意一点的是斐波那契数组超过500时,斐波那契数就超过100位了,所以开600的数组足矣
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String []args) {
BigInteger fib[] = new BigInteger[550];
fib[0] = BigInteger.valueOf(1);
fib[1] = BigInteger.valueOf(1);
for(int i = 2; i < 550; i++) {
fib[i] = fib[i-1].add(fib[i-2]);
}
BigInteger a;
BigInteger b;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
a = scanner.nextBigInteger();
b = scanner.nextBigInteger();
if(a.equals(BigInteger.ZERO)&&b.equals(BigInteger.ZERO))
break;
int sum = 0;
for(int i = 1; i < 550; i++) {
if(fib[i].compareTo(b)>0)
break;
if(a.compareTo(fib[i])<=0)
sum++;
}
System.out.println(sum);
}
}
}