题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1316
题目大意:计算在[a,b]之间存在多少个斐波那契数。
题目分析:要考虑a=0时的情况。首先还是要求出0~10^10存在多少个斐波那契数,大概是498个,第499个斐波那契数大概有105位了。
代码参考:
import java.util.*;
import java.math.*;
public class Main{
public static void main(String args[]){
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int i, j;
BigInteger f[] = new BigInteger[500];//java中定义数组的方式
BigInteger a, b;
f[1] = new BigInteger("1");//f[1]=1;
f[2] = new BigInteger("2");//f[2]=2;
for(i=3; i<500; ++i){
f[i] = f[i-1].add(f[i-2]);
}
while(true){
a = in.nextBigInteger();
b = in.nextBigInteger();
if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) break;
for(i=1; i<500; ++i){
if(f[i].compareTo(a) >=0) break;//大数之间的比较用compareTo()
}
for(j=i; j<500; ++j){
if(f[j].compareTo(b) >0) break;
}
System.out.println(j-i);
}
}
}