题解 | #高精度整数加法#

高精度整数加法

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);
                }
            }
        }
    }
}

全部评论

相关推荐

12-19 16:52
门头沟学院
点赞 评论 收藏
分享
11-04 19:05
已编辑
东莞城市学院 单片机
不知道怎么取名字_:你这个要实习两年?哪有这么久的,感觉就是即使你毕业了,但还按实习的话,是不是不用给你缴社保公积金啥的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务