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

高精度整数加法

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

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    String a1 = sc.nextLine();
    String a2 = sc.nextLine();

    String res = "";
    int addExtra = 0;
    int addRest = 0;

    char[] ar1 = a1.toCharArray();
    char[] ar2 = a2.toCharArray();

    int len = Math.max(ar1.length, ar2.length);

    for (int i = len - 1, j = ar1.length - 1, k = ar2.length - 1;
            i >= 0; i--, j--, k-- ) {
        int s1 = j >= 0 ? ar1[j] - '0' : 0;
        int s2 = k >= 0 ? ar2[k] - '0' : 0;
        addRest = (s1 + s2 + addExtra) % 10;
        addExtra = (s1 + s2 + addExtra) / 10;
        res = addRest + res;
        int bb = 1;
    }
    if (addExtra > 0) {
        res = addExtra + res;
    }
    System.out.println(res);

}

}

全部评论

相关推荐

牛客154160166号:9月底还给我发短信,好奇怪,我24届的
点赞 评论 收藏
分享
09-29 17:44
已编辑
蔚来_测(准入职员工)
//鲨鱼辣椒:见不了了我实习了四个月上周再投筛选了一天就给我挂了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务