uva 10183(大数)
题意:找a和b之间的斐波那契数有几个。
思路:尝试了一下java的大数。直接暴力即可。
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 class Main{ 5 public static int len; 6 7 public static void main(String arg[]){ 8 Scanner in = new Scanner(System.in); 9 BigInteger[] f = init(); 10 BigInteger a, b; 11 while(true){ 12 a = in.nextBigInteger(); b = in.nextBigInteger(); 13 if(a.compareTo(BigInteger.ZERO) == 0 && b.compareTo(BigInteger.ZERO) == 0) break; 14 int ans = 0; 15 for(int i=0; i<=len; i++){ 16 if(b.compareTo(f[i]) < 0) break; 17 if(a.compareTo(f[i]) > 0) continue; 18 ans++; 19 } 20 System.out.println(ans); 21 } 22 } 23 24 public static BigInteger [] init(){ 25 BigInteger [] f = new BigInteger[1000]; 26 f[0] = BigInteger.valueOf(1);f[1] = BigInteger.valueOf(2); 27 for(int i=2; i<1000; i++){ 28 f[i] = f[i-1].add(f[i-2]); 29 String tmp = f[i].toString(); 30 if(tmp.length() > 100) { 31 len = i; 32 break; 33 } 34 } 35 return f; 36 } 37 }
奔跑吧!少年!趁着你还年轻