UVa 10183 - How Many Fibs?

传送门

UVa 10183 - How Many Fibs?

思路

看到这么大的数字本来以为有别的办法的,看了题解才知道没有。

代码

import java.math.*;
import java.io.*;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigInteger fibo[] = new BigInteger[500];
        fibo[1] = BigInteger.ONE; fibo[2] = BigInteger.valueOf(2);
        for (int i = 3; i < 500; i++)
            fibo[i] = fibo[i - 1].add(fibo[i - 2]);
        while (cin.hasNext()) {
            BigInteger low = cin.nextBigInteger();
            BigInteger high = cin.nextBigInteger();
            if (low.compareTo(BigInteger.ZERO) == 0 && high.compareTo(BigInteger.ZERO) == 0)
                break;
            int cnt = 0;
            for (int i = 1; i < 500; i++) {
                if (fibo[i].compareTo(low) >= 0 && fibo[i].compareTo(high) <= 0)
                    cnt++;
            }
            System.out.println(cnt);
        }
    }
}

Powered by Jekyll and Theme by solid