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

高精度整数加法

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

}

}

全部评论

相关推荐

accaacc:2到4k,不是2k到4k,所以年薪是30块
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务