用的Java大整数类
单组测试数据:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger bigint1=in.nextBigInteger();
BigInteger bigint2=in.nextBigInteger();
System.out.println(bigint1.add(bigint2));//加法
System.out.println(bigint1.subtract(bigint2));//减法
System.out.println(bigint1.multiply(bigint2));//乘法
System.out.println(bigint1.divide(bigint2));//除法
System.out.println(bigint1.remainder(bigint2));//取余
System.out.println(bigint1.gcd(bigint2));//最大公约数
}
}
n组测试数据:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i < n; i++) {
BigInteger bigint1=in.nextBigInteger();
BigInteger bigint2=in.nextBigInteger();
System.out.println(bigint1.add(bigint2));//加法
System.out.println(bigint1.subtract(bigint2));//减法
System.out.println(bigint1.multiply(bigint2));//乘法
System.out.println(bigint1.divide(bigint2));//除法
System.out.println(bigint1.remainder(bigint2));//取余
System.out.println(bigint1.gcd(bigint2));//最大公约数
}
}
}