HD-ACM算法专攻系列(13)——How Many Fibs?

问题描述:

 

源码:

 

import java.math.BigInteger;
import java.util.*;

public class Main
{
		//主函数
        public static void main(String[] args)
        {
            BigInteger a, b, zero = BigInteger.valueOf(0), f1, f2, fn;
			int count;
			Scanner cin = new Scanner(System.in);
			while(true)
			{
				a = cin.nextBigInteger();
				b = cin.nextBigInteger();
				if(a.equals(zero) && b.equals(zero))break;
				count = 0;
				f1 = BigInteger.valueOf(1);
				if(a.compareTo(f1) <= 0 && b.compareTo(f1) >= 0)count++;
				f2 = BigInteger.valueOf(2);
				if(a.compareTo(f2) <= 0 && b.compareTo(f2) >= 0)count++;
				while(true)
				{
					fn = f2.add(f1);
					if(a.compareTo(fn) <= 0 && b.compareTo(fn) >= 0)count++;
					else if(b.compareTo(fn) < 0)break;
					f1 = f2;
					f2 = fn;
				}
				System.out.println(count);
			}
		}
}
 

  

posted @ 2017-10-07 15:38  OneForCheng  阅读(201)  评论(0编辑  收藏  举报