• Java大数BigInteger-用法记录


    在处理数据比较大的题目的时候,并且不允许提交python代码的情况下,可以采用Java大数来进行处理
    由于Java的内存回收机制等方面的原因,会导致Java的时间限制比其它语言的要大一些,一般情况下是其它语言的两倍
    比如说有这么一个题:
    在这里插入图片描述
    判断两个数那个大那个小,聪明的小朋友就用小学的知识将分母乘上去,但是看一眼数据范围的话,1e18 * 1e9是行不通的,当然这个题也可以用模拟的方式来进行处理,但是这里用Java大数的方式来处理一下
    Java的大数分为两种,一种是整数类型的,一种是小数类型的BigDecimal,一种是整数类型的BigInteger
    本篇文章就讲一下Java中BigInteger的使用

    提交代码

    1. 在提交Java代码的时候,不能含有导入的包package的名字
    2. 在新建Java类的过程中,一般情况下要将类命名为Main 否则再提交代码之后会报错
    3. 提交的代码中一定要有
    import java.math.BigInteger;
    import java.util.Scanner;
    

    这种代码,其中第一个是BigInteger 所在的jar 包
    对于上面这道题的代码,可以提交为:

    import java.math.*;
    import java.util.*;
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
            while(sc.hasNext()) {
            	long x = sc.nextLong();
            	long a = sc.nextLong();
            	long y = sc.nextLong();
            	long b = sc.nextLong();
            	
            	BigInteger xx = BigInteger.valueOf(x);
            	BigInteger aa = BigInteger.valueOf(a);
            	BigInteger yy = BigInteger.valueOf(y);
            	BigInteger bb = BigInteger.valueOf(b);
            	if(xx.multiply(bb).compareTo(yy.multiply(aa)) == 1) System.out.println(">");
            	else if(xx.multiply(bb).compareTo(yy.multiply(aa)) == -1) System.out.println("<");
            	else System.out.println("=");
            }
    	}
    }
    
    

    使用方式

    构造一个对象

    常用的有两种方式,一种是用字符串构造出来,一种是用long类型的证书构造出来

    BigInteger a = new BigInteger("123");
    BigInteger b = BigInteger.valueOf(456);
    

    对于要输入的一个BigInteger类型的数,则可以采用如下方式进行构造出来

    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger c = cin.nextBigInteger();
        }
    }
    

    加 add

    a = a.add(b);
    

    减 subtract

    a = a.subtract(b);
    

    乘 multiply

    a = a.multiply(b);
    

    除 divide

    a = a.divide(b);
    

    对于上面的总共的代码:

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
            BigInteger c = cin.nextBigInteger();
            System.out.println(a);
            System.out.println(b);
            System.out.println(c);
            a = a.add(b);
            System.out.println(a);
            a = a.subtract(b);
            System.out.println(a);
            a = a.multiply(b);
            System.out.println(a);
            a = a.divide(b);
            System.out.println(a);
        }
    }
    
    

    在这里插入图片描述
    在输入了789之后,可以见到如下的加减成熟之后的结果
    一定要注意在操作的过程中不要忘记对一个数重新赋值
    比如在要将a * b的时候,一定要写

    a = a.multiply(b);
    

    而不是

    a.multiply(b);
    

    这样不会报错,但是并没有改变变量a的值

    gcd 最大公约数

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
    
            BigInteger gcd = a.gcd(b);
            System.out.println(gcd);
        }
    }///对应输出为3
    
    

    lcm 最小公倍数

    a * b == gcd(a,b) * lcm(a,b)

    mod %

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
    
            BigInteger mod = a.mod(BigInteger.valueOf(11));
            System.out.println(mod);
        }
    }
    
    

    pow ^次方

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
    
            BigInteger pow = a.pow(2);
            System.out.println(pow);
        }
    }
    
    

    在这里插入图片描述

    abs 绝对值

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
    
            a = a.multiply(BigInteger.valueOf(-1));
            System.out.println(a);
            a = a.abs();
            System.out.println(a);
        }
    }
    
    

    对应输出为:
    在这里插入图片描述

    开方sqrt

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
    
            a =  a.sqrt();
            System.out.println(a);
        }
    }
    
    

    对应输出为:
    在这里插入图片描述

    modPow 次方取余

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
    
            a = a.modPow(BigInteger.valueOf(2),BigInteger.valueOf(100));
            System.out.println(a);
        }
    }
    
    

    对应输出为:
    在这里插入图片描述

    equals判断是否相等

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
    
            if(a.equals(b)) System.out.println("1: equal");
            else System.out.println("1: not equal");
            a = BigInteger.valueOf(456);
            if(a.equals(b)) System.out.println("2: equal");
            else System.out.println("1: not equal");
        }
    }
    
    

    对应输出为:
    在这里插入图片描述

    compareTo 比较大小

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            BigInteger a = new BigInteger("123");
            BigInteger b = BigInteger.valueOf(456);
            int flag = 0;
            flag = a.compareTo(b);
            System.out.println("a:" + a + "--" + b + "  " + flag);
            a = b;
            flag = a.compareTo(b);
            System.out.println("a:" + a + "--" + b + "  " + flag);
            b = BigInteger.valueOf(123);
            flag = a.compareTo(b);
            System.out.println("a:" + a + "--" + b + "  " + flag);
        }
    }
    
    

    对应输出为:
    在这里插入图片描述

    前一个数大于后面的数 结果为1
    两数相等返回结果为0
    后面的数大于前面的数 结果为-1

    常用的就上面这些啦

  • 相关阅读:
    移动项目开发笔记(禁止一个按钮在服务器事件处理完成前连续点击按钮)
    网页设计div+css之id与class使用原则
    Windows Media Encode 9简介及SDK
    酒桌上的规矩,社会的潜规则
    实现最小宽度的几种方法及CSS Expression
    Css的zindex属性与Flash动画层叠需注意
    asp.net很有用的字符串操作类
    生活中的几种心态
    Silverlight监测工具:Silverlight Spy
    silverlight为控件注册属性
  • 原文地址:https://www.cnblogs.com/PushyTao/p/15101031.html
Copyright © 2020-2023  润新知