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

高精度整数加法

https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            StringBuilder sb = new StringBuilder();
            int i = s1.length() - 1, j = s2.length() - 1, res = 0;
            for ( ; i >= 0 || j >= 0; i--, j--) {
                char ch1 = '0';
                if (i >= 0) {
                    ch1 = s1.charAt(i);
                }
                char ch2 = '0';
                if (j >= 0) {
                    ch2 = s2.charAt(j);
                }
                int n = (ch1 - '0') + (ch2 - '0') + res;
                sb.append(n % 10);
                res = n / 10;
            }
            if (res == 1) {
                sb.append('1');
            }
            System.out.println(sb.reverse().toString());
        }
    }
}

全部评论

相关推荐

星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 13:46
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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