题目变体:大数相加
思路
1.反转两个字符串,便于从低位到高位相加和最高位的进位导致和的位数增加;
2.对齐两个字符串,即短字符串的高位用‘0’补齐,便于后面的相加;
3.从头遍历,把两个正整数的每一位都相加,并加上进位;
4.最高位有进位则补上进位;
5.逆序输出;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
//反转字符串
String n1 = new StringBuffer(s1).reverse().toString();
String n2 = new StringBuffer(s2).reverse().toString();
int l1 = n1.length();
int l2 = n2.length();
int maxL = l1>l2?l1:l2;
//补齐0
if (l1 < l2) {
for (int i = l1; i < l2; i++) {
n1 += "0";
}
}else {
for (int i = l2; i < l1; i++) {
n2 += "0";
}
}
//System.out.println(n1);//test
//System.out.println(n2);//test
StringBuffer res = new StringBuffer();//存放的结果
int c = 0;//进位
for (int i = 0; i < maxL; i++) {
int nSum = Integer.parseInt(n1.charAt(i) + "") + Integer.parseInt(n2.charAt(i) + "") + c;
int ap = nSum%10;
res.append(ap);
c = nSum/10;
}
if (c>0) {
res.append(c);
}
//System.out.println(res);//test
System.out.println(res.reverse());
}