java大数的基本函数

来源:互联网 发布:java 获取locale 编辑:程序博客网 时间:2024/05/18 17:59
1.读入
Scanner cin=new Scanner(System.in);// 读入
while(cin.hasNextInt()) //等同于!=EOF,第一数一定要输入整形的
{
}

大数的一般是:
while(cin.hasNextBigInteger())  //第一个数一定要输入大数的
{
}

while(t-->0)   //等同于while(t--)
{
}

2.赋值
 BigInteger b=BigInteger.valueOf(a); //a可为int,long,string

3.四则运算
add(); 相加
subtract(); 相减
multiply(); 相乘
divide(); 相除

4.各种函数
pow();   a.pow(b)=a^b
gcd();   最大公约数
abs(); 绝对值
negate(); 取反数
remainder(); 取余
mod(); a.mod(b)=a%b=a.remainder(b);
max(); 
min();
punlic int comareTo();
boolean equals(); 是否相等
and();  例如a.and(b),计算a&b
doubleValue();  转化为double类型
longValue();  转化为long类型
floatValue();   转化为float类型
intValue();    转化为int类型
divideAndRemainder(BigInteger val)    返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
boolean isProbablePrime(BigInteger n)  判断大数是否为素数   
modPow(BigInteger n, BigInteger mod)   计算this^n % mod
nextProbablePrime(BigInteger n)   返回比大数n大的为素数的数
not()   计算  ~this
or(BigInteger a)   计算this|a
xor(BigInteger val)    返回其值为 (this ^ val) 的 BigInteger。
probablePrime(int bitLength, Random rnd)   返回有可能是素数的、具有指定长度的正 BigInteger。
shiftLeft(int n)    返回其值为 (this << n) 的 BigInteger。
shiftRight(int n)     返回其值为 (this >> n) 的 BigInteger。
toString(int radix)    返回此 BigInteger 的给定基数的字符串表示形式,默认十进制。
BigInteger p=p.stripTrailingZeros(); //去掉后导0  
String s=p.toPlainString(); //不让其变成科学计数法的表示法,变成一般的小数表示
if(s.startsWith("0.")) s=s.substring(1); //与前导的字符串比较  

5.基本常量:
A=BigInteger.ZERO   0
B=BigInteger.ONE    1
C=BigInteger.TEN    10


0 0