题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
高精度整数用BigInteger来做。
组织输入需要考虑一下。
import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.regex.Pattern; public class HightIntegerPlus { private static Pattern pattern = Pattern.compile("^[0-9]+$"); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<String> inputList = new ArrayList<>(); while (scanner.hasNext()) { String nextLine = scanner.nextLine(); if (pattern.matcher(nextLine).matches()) { if (inputList.size() == 1) { inputList.add(nextLine); String op1 = inputList.get(0); String op2 = inputList.get(1); BigInteger bi1 = new BigInteger(op1); BigInteger bi2 = new BigInteger(op2); System.out.println(bi1.add(bi2)); } else { inputList.add(nextLine); } } } } }