zoukankan      html  css  js  c++  java
  • HDU 1316 How Many Fibs?(java,简单题,大数)

    题目


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

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

    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 2112 HDU Today
    HDU 1869 六度分离
    HDU 3790 最短路径问题
    HDU2066 一个人的旅行
    HDU1596 find the safest road(最短路)
    HDU 1254 推箱子(双重bfs)
    HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
    HDU 1045 Fire Net
    数据结构之单链表头插法,尾插法
    Java--会移动、反弹的球
  • 原文地址:https://www.cnblogs.com/laiba2004/p/3702634.html
Copyright © 2011-2022 走看看