public static void add(String s1, String s2) {
int[] x = new int[Math.max(s1.length(), s2.length())];
int i = 0, j = Math.max(s1.length(), s2.length()) - Math.min(s1.length(), s2.length());
if (s1.length() > s2.length()) {
for (; i < j; i++) {
int t = Integer.parseInt(String.valueOf(s1.charAt(i)));
x[i] = t;
}
for (j = 0; i < s1.length() & j < s2.length(); i++, j++) {
int t1 = Integer.parseInt(String.valueOf(s1.charAt(i)));
int t2 = Integer.parseInt(String.valueOf(s2.charAt(j)));
x[i] = t1 + t2;
}
} else {
for (; i < j; i++) {
int t = Integer.parseInt(String.valueOf(s2.charAt(i)));
x[i] = t;
}
for (j = 0; i < s2.length() & j < s1.length(); i++, j++) {
int t1 = Integer.parseInt(String.valueOf(s2.charAt(i)));
int t2 = Integer.parseInt(String.valueOf(s1.charAt(j)));
x[i] = t1 + t2;
}
}
// 处理进位
for (i = x.length - 1; i > 0; i--) {
x[i - 1] = x[i - 1] + x[i] / 10;
x[i] = x[i] % 10;
}
String res = new String();
for (i = 0; i < x.length; i++) {
res += x[i];
}
x = null;
System.out.println("加法结果:" + res);
}
大数据的加法运算Java代码实现
最新推荐文章于 2021-07-15 22:59:28 发布