import java.util.Scanner; public class Test { public static void main(String[] args) { String str = add("4324242342", "246"); System.out.println(str); } private static String add(String s1, String s2) { StringBuffer result = new StringBuffer(); s1 = new StringBuffer(s1).reverse().toString(); s2 = new StringBuffer(s2).reverse().toString(); int len1 = s1.length(); int len2 = s2.length(); int maxlen = len1 > len2 ? len1 : len2; boolean addTarget = false; int addNum = 0; if (len1 < len2) { for (int x = len1; x < len2; x++) { s1 += "0"; } } else if (len1 > len2) { for (int x = len2; x < len1; x++) { s2 += "0"; } } for (int x = 0; x < maxlen; x++) { char c1 = s1.charAt(x); char c2 = s2.charAt(x); String sc1 = c1 + ""; String sc2 = c2 + ""; int nSum = Integer.parseInt(sc1) + Integer.parseInt(sc2); if ((nSum + addNum )>= 10) { if (x == maxlen - 1) addTarget = true; result.append(nSum - 10 +addNum); addNum = 1; } else { result.append(nSum +addNum); addNum = 0; } } if (addTarget) { result.append(1); } return result.reverse().toString(); } }
点赞 评论

相关推荐

10-25 12:05
已编辑
湖南科技大学 Java
若梦难了:我有你这简历,已经大厂乱杀了
点赞 评论 收藏
分享
牛客717484937号:双飞硕没实习挺要命的
点赞 评论 收藏
分享
牛客网
牛客企业服务