题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1316
计算给定的区间有多少个FIB数列个数
直接用暴力,但暴力也是有技巧的,直接参考代码中有注释
源代码:
import java.math.BigInteger; import java.util.Scanner; //125MS 3668K AC public class Main{ static BigInteger[] f=new BigInteger[1000];//1000就够了 public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int count; 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 (scanner.hasNext()) { count=0; BigInteger a,b; a=scanner.nextBigInteger(); b=scanner.nextBigInteger(); if (BigInteger.ZERO.equals(a)&& BigInteger.ZERO.equals(b)) { break; } for (int i = 1; i < 500; i++) { //因为Fib数列是递增的所以只要判断比小区间大于或者等于 比大区间的小于或者等于就行 if (a.compareTo(f[i])<=0 && b.compareTo(f[i])>=0 ) { count++; } } System.out.println(count); } } }