现将前1000个的斐波那契数打表,然后再找就好了。
代码:
import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger[] s = new BigInteger[1005]; s[1] = new BigInteger("1"); s[2] = new BigInteger("2"); int i = 3; while(i < 1000){ s[i] = s[i-1].add(s[i-2]); //System.out.println(s[i]); i++; } BigInteger a, b, temp; temp = new BigInteger("0"); while(cin.hasNextBigInteger()){ a = cin.nextBigInteger(); b = cin.nextBigInteger(); if(a.compareTo(temp)== 0&&b.compareTo(temp)== 0) break; int ans = 0; i= 1; while((s[i].compareTo(a)) < 0){ i++;//System.out.println(i); } for(; s[i].compareTo(b) <= 0; i ++){ //System.out.println(i); ++ans; } System.out.println(ans); //a = cin.nextBigInteger(); //b = cin.nextBigInteger(); } } }
hdoj 1316 How Many Fibs? 【Java大数】+【打表】
原文地址:http://blog.csdn.net/shengweisong/article/details/39122663