BigInteger:
1、赋值:
1、BigInteger a=new BigInteger("1");
2、BigInteger b=BigInteger.valueOf(1);
3、A=BigInteger.ONE 1
4、B=BigInteger.TEN 10
5、C=BigInteger.ZERO 0
6、n.compareTo(BigInteger.ZERO)==0 //相当于n==0
7、if(a[i].compareTo(n)>=0 &&a[i].compareTo(m)<=0) // a[i]>=n && a[i]<=m
2、运算 :
大数的加减运算不同于普通整数的加减乘除运算
1 加—— a+b: a=a.add(b);
2 减—— a-b: a=a.subtract(b);
3 乘—— a*b: a=a.multiply(b);
4 除—— a/b: a=a.divide(b);
5 求余—a%b: a=a.mod(b);
6 转换—a=b: b=BigInteger.valueOf(a);
7 去反数—— -a:a.negate()
8 幂——a^b: a.pow(b)
9 绝对值——|c|:c.abc()
10 最大公约数——gcd(a,b)=a.gcd(b)
11比较 if (ans.compareTo(x) == 0) System.out.println("相等")
3.BigInteger与String:
1、将指定字符串转换为十进制表示形式;
BigInteger(String val);
2、将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger
BigInteger(String val,int radix);
3、BigInteger 的十进制字符串表示形式
BigInteger(str,radix)
4、 BigInteger 的给定基数的字符串表示形式
toString(int radix)
package Content; import java.util.*; import java.math.BigInteger; public class BigNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //大数类型不能直接赋值 BigInteger num1 = BigInteger.ONE; // <=> BigInteger num1 = BigInteger.valueOf(1); BigInteger num2 = BigInteger.valueOf(2); System.out.println("num1 = "+num1); System.out.println("num2 = "+num2); BigInteger a,b,c; BigInteger ans_add,ans_sub,ans_mul,ans_div,ans_mod,ans_gcd; a = scanner.nextBigInteger(); b = scanner.nextBigInteger(); c = scanner.nextBigInteger(); System.out.println("a = "+ a); System.out.println("b = "+ b); System.out.println("a反数="+a.negate()); System.out.println("|c|="+ c.abs()); System.out.println("c^2="+c.pow(2)); ans_add = a.add(b); //a+b ans_sub = a.subtract(b); //a-b ans_mul = a.multiply(b); //a*b ans_div = a.divide(b); //a/b ans_mod = a.mod(b); //a%b ans_gcd = a.gcd(b);// gcd(a,b) System.out.println("a + b = "+ans_add); System.out.println("a - b = "+ans_sub); System.out.println("a * b = "+ans_mul); System.out.println("a / b = "+ans_div); System.out.println("a % b = "+ans_mod); System.out.println("gcd(a,b)="+ans_gcd); //比较 if (a.compareTo(b) == 0) { System.out.println("相等"); }else { System.out.println("不相等"); } //BigInteger类型转换成Long类型或int类型问题 int i = a.intValue(); long l = b.longValue(); float f = a.floatValue(); double d = b.doubleValue(); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); //转换 :(十进制)String<=>BigInteger System.out.println("String=>BigInteger:"); String string1 = "12345"; BigInteger bigInteger1 = new BigInteger(string1); System.out.println("BigInteger = "+bigInteger1); System.out.println("BigInteger=>String:"); System.out.println(bigInteger1.toString()); //转换:(*进制)String<=>BigInteger String string2 = "1000"; BigInteger bigInteger2 = new BigInteger(string2,2); System.out.println("string="+string2); System.out.println("BigInteger = "+bigInteger2); System.out.println("BigInteger=>String:"); System.out.println(bigInteger1.toString(2)); } }
原文:https://www.cnblogs.com/Lemon1234/p/11619950.html