Java中的BigInteger - 260401
- [BigInteger - 大数类](#BigInteger - 大数类)
- 基础例题
BigInteger - 大数类
- 前置:
import java.Math.BigInteger; - 性质:是不可变类,每次作运算之后都会在计算机里创建一个新对象
- 构造:
BigInteger a = new BigInteger("123");从字符串创建
BigInteger a = BigInteger.valueOf(123L);从long数字创建 - 常用值表示:
BigInteger.ZEROBigInteger.ONEBigInteger.TWO - 常用函数:
a.add(b) :加法
a.subtract(b):减法
a.multiply(b):乘法
a.devide(b):除法
a.remainder(b):取余
a.pow():幂运算
a.gcd(b):最大公约数
a.compareTo(b):当 前>后 时返回1,反之返回-1,相等返回0
a.toString():变成字符串
基础例题
洛谷 P1009

- 代码实现
java
import java.util.*;
import java.math.BigInteger;
public class Main{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
long n = input.nextLong();
BigInteger sum = BigInteger.ZERO;
BigInteger pre = BigInteger.ONE;
for(int i=1;i<=n;i++){
pre = pre.multiply(BigInteger.valueOf(i));
sum = sum.add(pre);
}
System.out.print(sum.toString());
}
}
洛谷 P1303

- 代码实现
java
import java.util.*;
import java.math.BigInteger;
public class Main{
public static void main(String [] args){
Scanner input = new Scanner (System.in);
String a = input.nextLine();
String b = input.nextLine();
BigInteger aa = new BigInteger(a);
BigInteger bb = new BigInteger(b);
BigInteger ans = aa.multiply(bb);
System.out.print(ans.toString());
}
}