2021-04-22

本文介绍了Java中BigInteger类的使用,包括如何通过new BigInteger()创建大整数,以及进行加、减、乘、除、取余等操作。还提到了BigInteger的常用方法,如compareTo()、abs()和equals(),并给出了具体的代码示例,帮助理解其功能。
摘要由CSDN通过智能技术生成

写一期BigInterger

先写一个实例吧
如果:控制台输入一个很大的数s,你想去先储存它,然后用它,你不能用类型转换如(BigInterger).s来转换或者BigInterger.valuesOf(s).
应该用new BigInterger(s)来解决。

 String s="10000000000000000000000000000";
        BigInteger bigInteger = new BigInteger(s);
        System.out.println(bigInteger);

10000000000000000000000000000

Process finished with exit code 0

用它的话,就涉及加、减、乘、除、取余
public BigInteger add(BigInteger val):加
public BigInteger subtract(BigInteger val):减
public BigInteger multiply(BigInteger val):乘
public BigInteger divide(BigInteger val):除
remainder();取余
pow(); a.pow(b)=a^b a的b次方
gcd(); 最大公约数(各位,竞赛用它,不是有手就行?)
一些其他可能用到的方法
int compareTo(BigInteger val)
这个BigInteger与指定BigInteger比较。
BigInteger abs()
返回的值BigInteger是BigInteger的绝对值。

boolean equals(Object x)
与BigInteger指定对象比较,看是否相等。 在这里插入图片描述
记不住的话,直接.一下,直接起飞
话不多说,老规矩,上例子

  String s="10000000000000000000000000000";
        BigInteger bigInteger = new BigInteger(s);
        System.out.println(bigInteger.divide(new BigInteger("100000000000000")));
        System.out.println(bigInteger.add(new BigInteger("100000000000000")));
        System.out.println(bigInteger.subtract(new BigInteger("100000000000000")));
        System.out.println(bigInteger.multiply(new BigInteger("100000000000000")));
        System.out.println(bigInteger.not());//BigInteger not()返回的值是 (~this) BigInteger。简单说就是取反
100000000000000
10000000000000100000000000000
9999999999999900000000000000
1000000000000000000000000000000000000000000
-10000000000000000000000000001

Process finished with exit code 0

总结:
1.一般数据用int就行,长一点的用long
范围:
byte (-128-127);

long (-9223372036854774808~9223372036854774807)
2.以下是java.math.BigInteger类中的字段:

static BigInteger ONE – BigInteger的常量1。

static BigInteger TEN – BigInteger的常量10。

static BigInteger ZERO – BigInteger的常量0。
如果用BigInterger则需要知道那些方法直接看API。
其他还有如进制转换,可以各位拓展一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值