蓝桥杯系列二:Java常用API-大数类

1. ⭐️背景⭐️

Java中的基础数据类型能存储的最大的二进制数是 2 ^ 63 - 1,只要运算过程中会超过这个数,就会造成数据溢出,就会造成错误。但在Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大。

2. ⭐️介绍⭐️

📒这两个类都在java.math.*包中,因此每次必须在开头处引用该包;
📒BigInteger实现了任意精度的整数运算;
📒BigDecimal实现了任意精度的浮点数运算。

3. ⭐️方法⭐️

📚普通的数值转换成大数值

分别调用BigInteger.valueOf("普通数值"),BigDecimal.valueOf("普通数值")即可。

int a=10;
BigInteger.valueOf(a2);
double a=1.123456;
BigDecimal.valueOf(a);

📚创建

import java.math.BigDecimal;
import java.math.BigInteger;

public class 创建 {
    public static void main(String[] args) {
        //1.直接声明
        BigInteger a;
        BigDecimal b;
        //2.使用构造函数初始化
        BigInteger a1 = new BigInteger("1234567891021212155646");
        BigDecimal b1 = new BigDecimal("123456.756231");

    }
}

📚赋值

使用

BigInteger.valueOf(long val);
BigDecimal.valueOf(double val);

注意:

val 不能超过 long 类型的最大取值9223372036854775807, 超过int时要在数后面加L(int的取值范围为: -231——231-1,即-2147483648——2147483647 )。

import java.math.BigDecimal;
import java.math.BigInteger;

public class 赋值 {
    public static void main(String[] args) {
        //BigInteger.valueOf(long val);
        //BigDecimal.valueOf(double val);
        BigInteger a;
        BigDecimal b;
        //注意 val 不能超过 long 类型的最大取值9223372036854775807, 超过int时要在数后面加L如:
        a = BigInteger.valueOf(123456789101112L); //大于int范围的要加L
        b = BigDecimal.valueOf(123456.12341235); // 超出的小数位数会自动舍弃

    }
}

或者使用

BigInteger a;
BigInteger b = new BigInteger("123456");
a = b;
import java.math.BigInteger;

public class 赋值二 {
    public static void main(String[] args) {
        BigInteger a;
        BigInteger b = new BigInteger("123456");
        a = b;
        System.out.print(a);//123456
    }
}

📚加法

方法add();

import java.math.BigInteger;

public class 加法 {
    public static void main(String[] args) {
        BigInteger a, b, c;
        a = BigInteger.valueOf(123456789); // 赋值为 123456789
        b = BigInteger.valueOf(987654321); // 赋值为 987654321
        c = a.add(b);
        System.out.print(c);
    }
}

📚减法

方法subtract();

import java.math.BigInteger;

public class 减法 {
    public static void main(String[] args) {
        BigInteger a, b, c;
        a = BigInteger.valueOf(123456789); // 赋值为 123456789
        b = BigInteger.valueOf(987654321); // 赋值为 987654321
        c = a.subtract(b);
        System.out.print(c);

    }
}

📚乘法

方法multiply();

import java.math.BigInteger;

public class 乘法 {
    public static void main(String[] args) {
        BigInteger a, b, c;
        a = BigInteger.valueOf(123456789); // 赋值为 123456789
        b = BigInteger.valueOf(987654321); // 赋值为 987654321
        c = a.multiply(b);
        System.out.print(c);

    }
}

📚除法

方法divide();

import java.math.BigInteger;

public class 除法 {
    public static void main(String[] args) {
        BigInteger a, b, c;
        a = BigInteger.valueOf(987654321); // 赋值为 987654321
        b = BigInteger.valueOf(123456789); // 赋值为 123456789
        c = a.divide(b); // 整数相除仍为整数
        System.out.print(c);

    }
}

📚进制转化

使用构造函数BigInteger(String, int index),可以把一个index进制的字符串,转化为10进制的BigInteger。

BigInteger a = new BigInteger("111110", 2);111110从二进制变为10进制赋值给a

a.toString(radix);//把十进制数a转变为radix数进制。

System.out.println(a.toString(16));把a转化为16进制的字符串输出

综合案例:
给定n个十六进制正整数,输出它们对应的八进制数。
答案:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i=1;i<=n;i++){
            String s = sc.next();
            String s1 = new BigInteger(s,16).toString(8);
            System.out.println(s1);
        }
    }
}

📚求最值

最大值:max();
最小值:min();
BigInteger a, b, c, d;
a = BigInteger.valueOf(987654321);
b = BigInteger.valueOf(12345678); 
c = a.max(b); //a,b中的最大值
d = a.min(b); //a,b中的最小值
System.out.println(c);
System.out.println(d);

输出:

987654321
12345678

📚取余

方法mod();

BigInteger a, b, c;
a = BigInteger.valueOf(987654321); // 赋值为 987654321
b = BigInteger.valueOf(123456789); // 赋值为 123456789
c = a.mod(b);
System.out.print(c);

输出:

9
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值