/**
* @Author: DiTian
* @Description: 两字符串相加
* @Date: Created in 15:23 2021/8/12
*/
public class StrAddStr {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str1 = scanner.nextLine();
String str2 = scanner.nextLine();
String result = AddTwoStr(str1,str2);
System.out.println(result);
}
private static String AddTwoStr(String str1, String str2) {
int i = str1.length()-1,j = str2.length()-1;
int add = 0; //保存进位
int result;
StringBuffer ans = new StringBuffer();
while (i>=0||j>=0||add>0){
int x = i>=0?str1.charAt(i)-'0':0;
int y = j>=0?str2.charAt(j)-'0':0;
result = x+y+add;
ans.append(result%10);
add = result/10;
i--;
j--;
}
ans.reverse();
return ans.toString();
}
}
415. 字符串相加
最新推荐文章于 2024-11-02 15:52:57 发布