题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String str2 = br.readLine();
String[] strArr = str.split("");
String[] str2Arr = str2.split("");
String[] maxStr = strArr.length > str2Arr.length ? strArr : str2Arr;
String[] minStr = strArr.length > str2Arr.length ? str2Arr : strArr;
String[] copy = new String[maxStr.length];
for (int i = 0; i < maxStr.length; i++) {
if (i >= maxStr.length - minStr.length) {
copy[i] = minStr[i - maxStr.length + minStr.length];
} else {
copy[i] = "0";
}
};
StringBuffer sb = new StringBuffer();
int plus = 0;
for (int j = maxStr.length - 1; j >= 0; j--) {
int a = Integer.parseInt(maxStr[j]);
int b = Integer.parseInt(copy[j]);
int c = a + b + plus;
if (c >= 10 && j != 0) {
sb.append(String.valueOf(c).split("")[1]);
plus = 1;
} else if (c >= 10 && j == 0) {
sb.append(new StringBuffer(String.valueOf(c)).reverse());
plus = 1;
} else {
sb.append(c);
plus = 0;
}
}
System.out.print(sb.reverse().toString());
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String str2 = br.readLine();
String[] strArr = str.split("");
String[] str2Arr = str2.split("");
String[] maxStr = strArr.length > str2Arr.length ? strArr : str2Arr;
String[] minStr = strArr.length > str2Arr.length ? str2Arr : strArr;
String[] copy = new String[maxStr.length];
for (int i = 0; i < maxStr.length; i++) {
if (i >= maxStr.length - minStr.length) {
copy[i] = minStr[i - maxStr.length + minStr.length];
} else {
copy[i] = "0";
}
};
StringBuffer sb = new StringBuffer();
int plus = 0;
for (int j = maxStr.length - 1; j >= 0; j--) {
int a = Integer.parseInt(maxStr[j]);
int b = Integer.parseInt(copy[j]);
int c = a + b + plus;
if (c >= 10 && j != 0) {
sb.append(String.valueOf(c).split("")[1]);
plus = 1;
} else if (c >= 10 && j == 0) {
sb.append(new StringBuffer(String.valueOf(c)).reverse());
plus = 1;
} else {
sb.append(c);
plus = 0;
}
}
System.out.print(sb.reverse().toString());
}
}