1. 题目
读题
考查点
2. 解法
思路
代码逻辑
具体实现
public class HJ57 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num1 = sc.nextLine();
String num2 = sc.nextLine();
System.out.println(addStr(num1, num2));
}
public static String addStr(String num1, String num2) {
StringBuffer sb = new StringBuffer();
int sum = 0;
int carry = 0;
int i = num1.length() - 1, j = num2.length() - 1;
while (i >= 0 || j >= 0 || carry > 0) {
sum += carry;
if (i >= 0) {
sum += num1.charAt(i) - '0';
i--;
}
if (j >= 0) {
sum += num2.charAt(j) - '0';
j--;
}
sb.append(sum % 10);
carry = sum / 10;
sum = 0;
}
return sb.reverse().toString();
}
}