首页 > 代码库 > hdoj 1316 How Many Fibs? 【Java大数】+【打表】

hdoj 1316 How Many Fibs? 【Java大数】+【打表】

现将前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();
        }
    }
}

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1316

hdoj 1316 How Many Fibs? 【Java大数】+【打表】