码迷,mamicode.com
首页 > 编程语言 > 详细

HDU 1316 How Many Fibs?(java,简单题,大数)

时间:2014-05-01 20:53:42      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   ext   color   get   int   string   

题目

 


/**
* compareTo:根据该数值是小于、等于、或大于 val 返回 -1、0 或 1;
public int compareTo(BigInteger val)
将此 BigInteger 与指定的 BigInteger 进行比较。
对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=)
中的每一个运算符的各个方法,优先提供此方法。
执行这些比较的建议语句是:(x.compareTo(y) <op> 0),
其中 <op> 是六个比较运算符之一。
*/

/**
* and:等同于c++的&&,且;
*/

 

bubuko.com,布布扣
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

    /**
     * @xqq
     */
    public int an(BigInteger a, BigInteger b, BigInteger sa, BigInteger sb) {
        int ans = 0;
        if(a.compareTo(sa) >= 0 && a.compareTo(sb) <= 0) {
            ans++;
        }
        if(b.compareTo(sa) >= 0 && b.compareTo(sb) <= 0) {
            ans++;
        }
        for(;;) {
            BigInteger c = a.add(b);
            a = b;
            b = c;
            if(b.compareTo(sa) >= 0 && b.compareTo(sb) <= 0) {
                ans++;
            }
            if(b.compareTo(sb) > 0) {
                return ans;
            }
        }
        
    }
    public static void main(String[] args)    throws Exception {
        // 定义并打开输入文件
        Scanner cin = new Scanner(System.in);
        
        Main e = new Main();
        BigInteger a = BigInteger.valueOf(1);
        BigInteger b = BigInteger.valueOf(2);
        BigInteger sa;
        BigInteger sb;
        BigInteger zero = BigInteger.ZERO;
        
        while(cin.hasNext()) {
            sa = cin.nextBigInteger();
            sb = cin.nextBigInteger();
            if(sa.compareTo(zero) == 0 && sb.compareTo(zero) == 0) {
                break;
            }
            System.out.println(e.an(a, b, sa, sb));
        }
        
        cin.close();  //关闭输入文件
    }
}
View Code

 

HDU 1316 How Many Fibs?(java,简单题,大数),布布扣,bubuko.com

HDU 1316 How Many Fibs?(java,简单题,大数)

标签:style   blog   class   code   java   tar   ext   color   get   int   string   

原文地址:http://www.cnblogs.com/laiba2004/p/3702634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!