题解 | #高精度整数加法#10进制
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
String b = in.nextLine();
int aIndex = a.length() - 1;
int bIndex = b.length() - 1;
String res = "";
int carry = 0;
while (true) {
if (aIndex < 0 && bIndex < 0) {
if (carry > 0) {
res = carry + res;
}
break;
}
int aa = 0;
if (aIndex >= 0) {
aa = a.charAt(aIndex) - '0';
}
int bb = 0;
if (bIndex >= 0) {
bb = b.charAt(bIndex) - '0';
}
int sum = aa + bb + carry;
res = (sum % 10) + res;
carry = sum / 10;
aIndex--;
bIndex--;
}
System.out.println(res);
}
}
}
