题解 | #高精度整数加法#
高精度整数加法
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()); } } }