• 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
    一道又一道,好高兴!
  • 相关阅读:
    Docker技术入门之---Docker核心概念:镜像、容器、仓库(2)
    Docker技术入门之---Docker安装(1)
    微信小程序自定义组件
    vue项目优化--使用CDN和Gzip
    小程序分包
    VUE--- browserHistory 和 hashHistory。
    vue-生成的css文件中背景url()图片路径问题
    vue异步组件
    Vue双向绑定原理及实现
    web-view小程序转发功能,web-view和小程序之间传参
  • 原文地址:https://www.cnblogs.com/laiba2004/p/3702634.html
Copyright © 2020-2023  润新知