Java大整数运算BigInteger


import java.math.BigInteger;
BigInteger 对象的创建

BigInteger a = new BigInteger("123"); // 这里是字符串
String str = "123";
BigInteger a = BigInteger.valueOf(str);

int num = 456;
BigInteger a = BigInteger.valueOf(num);
基本常量
a = BigInteger.ONE // 1
b = BigInteger.TEN // 10
c = BigInteger.ZERO // 0
BigInteger 的输入输出
直接读入 BigInteger

Scanner in = new Scanner(System.in); 
while(in.hasNext()) //等同于!=EOF
{
    BigInteger a;
    a = in.nextBigInteger();
    System.out.print(a.toString());
}
间接读入 BigInteger

Scanner in = new Scanner(System.in); 
while(in.hasNext()) //等同于!=EOF
{
    String s = in.nextLine();
    BigInteger a = new BigInteger(s);
    System.out.print(a.toString());
}
BigInteger 直接输出

System.out.print(a);
BigInteger 转化成十进制表示的 String

System.out.print(a.toString());

BigInteger 转化成 p 进制表示的 String
int p = 2;
System.out.print(a.toString(p)); // 输出a的二进制

BigInteger 二进制下的长度
BigInteger n = new BigInteger("12");
System.out.println(n.bitLength()); // 4

BigInteger 之间的比较
BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");
System.out.println(a.equals(b)); // a == b 时为 true 否则为 false
if(a.compareTo(b) == 0) System.out.println("a == b"); // a == b
else if(a.compareTo(b) > 0) System.out.println("a > b"); // a > b
else if(a.compareTo(b) < 0) System.out.println("a < b"); // a < b
基本运算
比较
a.compareTo(b) == 0
a.compareTo(b) > 0 
a.compareTo(b) < 0

加法
a.add(b)

减法
a.subtract(b)

乘法
a.multiply(b)

除法
a.devide(b)

取余
a.remainder(b)

绝对值
a.abs()

取反
a.negate()

幂
a.pow(3)

intValue()   
longValue()将此BigInteger转换为longsqrt()  返回此BigInteger的整数平方根。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁de小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值