zoukankan      html  css  js  c++  java
  • java大数运算(讲解)

    在算法竞赛或者面试中我们经常遇到大数问题,例如求一个很大的阶层,大数加法等等。

    住在这种情况下我们用常规解法(使用long long或long long int)肯定是不行的,

    而我们自己用c/c++写一个大数的算法又过于麻烦且易于出错,

    在这种情况下使用java中自带的大数类是我们最好的选择,

    相对比c/c++比较而言,java语言写大数是比较流氓的,但是代码量非常的少,而且容易理解,

    你只需要调包就可以了。
    BigInteger

    package 大数;
    
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class 大数 {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            BigInteger b1=new BigInteger("123456789");
            BigInteger b2=new BigInteger("987654321");
            System.out.println("加法操作:"+b2.add(b1));
            System.out.println("减法操作:"+b2.subtract(b1));
            System.out.println("乘法操作:"+b2.multiply(b1));
            System.out.println("除法操作:"+b2.divide(b1));
            System.out.println("最大数:"+b2.max(b1));
            System.out.println("最小数:"+b2.min(b1));
            BigInteger result[]=b2.divideAndRemainder(b1);
            System.out.println("商是:"+result[0]+" "+"余数是:"+result[1]);
        }
    
    }
    

    1.定义常用方法:

    BigInteger a=new BigInteger(“123”);  //第一种,参数是字符串
    BigInteger a=BigInteger.valueOf(123);    //第二种,参数可以是int、long
    
    

    2.大整数比较大小

    a.equals(b);  //如果a、b相等返回true否则返回false
    a.compareTo(b);  //a小于b返回-1,等于返回0,大于返回1
    

    3.大数常用方法及常量

    a.mod(b);  //求余
     a.gcd(b);  //求最大公约数
     a.max(b);  //求最大值
     a.min(b);  //求最小值
    BigInteger.ZERO    //大整数0
    BigInteger.ONE    //大整数1
    BigInteger.TEN   //大整数10
    //先转换成字符串再求字符串的长度
    a.toString().length();   //a的类型为BigInteger
    

    BigDecimal
    使用此类可以完成大的小数操作,而且也可以使用此类进行精确的四舍五入,这一点在开发中经常使用。
    对于不需要任何准确计算精度的程序可以直接使用float或double完成,但是如果需要精确计算结果,则必须使用BigDecimal类。

    //最重要的方法
    a.compareTo(b);  //比较两个浮点数是否相等
    
    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class 大数 {
        public static void main(String[] args) {
            System.out.println("加法运算:" + MyMath.round(MyMath.add(10.345, 3.333), 1));
            System.out.println("减法运算:" + MyMath.round(MyMath.sub(10.345, 3.333), 3));
            System.out.println("乘法运算:" + MyMath.round(MyMath.mul(10.345, 3.333), 4));
            System.out.println("除法运算:" + MyMath.div(10.345, 3.333, 3));
        }
    }
    
    class MyMath {
        public static double add(double d1, double d2) { // 进行加法计算
            BigDecimal b1 = new BigDecimal(d1);
            BigDecimal b2 = new BigDecimal(d2);
            return b1.add(b2).doubleValue();
        }
    
        public static double sub(double d1, double d2) { // 进行减法计算
            BigDecimal b1 = new BigDecimal(d1);
            BigDecimal b2 = new BigDecimal(d2);
            return b1.subtract(b2).doubleValue();
        }
    
        public static double mul(double d1, double d2) { // 进行乘法计算
            BigDecimal b1 = new BigDecimal(d1);
            BigDecimal b2 = new BigDecimal(d2);
            return b1.multiply(b2).doubleValue();
        }
    
        public static double div(double d1, double d2, int len) { // 进行除法计算
            BigDecimal b1 = new BigDecimal(d1);
            BigDecimal b2 = new BigDecimal(d2);
            return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
    
        public static double round(double d, int len) { // 进行四舍五入
            BigDecimal b1 = new BigDecimal(d);
            BigDecimal b2 = new BigDecimal(1); // 技巧
            return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
    }
    

    大数素数了解:

    public static void main(String[] args)
        {
            Scanner cin = new Scanner(System.in);
            BigInteger x;
            x = cin.nextBigInteger();
            if(x.isProbablePrime(1))
                System.out.println("Yes");
            else
                System.out.println("No");
        }
    
  • 相关阅读:
    Shell-01-脚本开头实现自动添加注释
    Linux中通过SHELL发送邮件
    linux服务器修改密码登录Failed to restart ssh.service: Unit ssh.service not found
    ffmpeg+java实现五秒钟剪辑80个视频
    Vue学习-watch 监听用法
    springboot添加定时任务
    Spring异常:java.lang.NoClassDefFoundError: org/springframework/core/OrderComparator$OrderSourceProvider
    多线程实战-龟兔赛跑
    Git分支管理(二)
    android studio bug : aidl is missing 解决方案
  • 原文地址:https://www.cnblogs.com/julyzqy/p/12777431.html
Copyright © 2011-2022 走看看