JAVA大数处理

Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类。
这两个类都在java.math.*包中,因此每次必须在开头处引用该包。

基本常量
BigInteger.ONE   1
BigInteger.TEN    10
BigInteger.ZERO  0

BigInteger转为整型(浮点型类似):
BigInteger x;
int s=x.intValue();
long ss=x.longValue();

常用函数:
valueOf(long x);      将x转换为BigInteger并返回
add();           相加
subtract();    相减
multiply();    相乘
divide();        相除取整
remainder();   mod();   取余
pow();       次方
gcd();        最大公约
abs();        绝对值
max(); min();   最大最小
compareTo();    返回-1,0,1 小于、等于、大于
equals();    相等

构造函数:
BigInteger(String)     将字符串以十进制方式存储为大数
BigInteger(String val,int radix);   字符串val的基数为radix,例如val="11",radix=2,则大数的十进制为3

不常用函数:
and();  or(); not();  与、或、取反
negate(); 取相反数
toString(int radix);   返回对象的radix进制的字符串
BigInteger[] divideAndRemainder(BigInteger val)    用数组返回商和余数
boolean isProbablePrime(BigInteger n)      判断大数是否为素数
nextProbablePrime(BigInteger n)     返回比大数n大的为素数的数
modPow(BigInteger n, BigInteger mod) 计算this^n % mod
shiftLeft(int n) 返回其值为 (this << n) 的 BigInteger
shiftRight(int n)  返回其值为 (this >> n) 的 BigInteger

输入输出
nextBigInteger(radix); radix进制形式输入

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			BigInteger x = cin.nextBigInteger();
			x = x.add(BigInteger.valueOf(10)).subtract(new BigInteger("5"));//x=x+10-5
			System.out.println(x);
			System.out.println(x.toString());
		}

	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值