public class Cals {
static byte temp = 0;
static byte temp2 = 0;
public static void main(String[] args) {
String num1 = "1111";
String num2 = "111";
char[] nu1 = num1.toCharArray();
char[] nu2 = num2.toCharArray();
int len = nu1.length > nu2.length ? nu1.length + 1 : nu2.length + 1;
byte[] n1 = new byte[len];
byte[] n2 = new byte[len];
for (int i = nu1.length - 1; i >= 0; i--) {
n1[len - (nu1.length - i)] = (byte) (nu1[i] - 48);
}
for (int i = nu2.length - 1; i >= 0; i--) {
n2[len - (nu2.length - i)] = (byte) (nu2[i] - 48);
}
String re = "";
for (int i = len - 1; i >= 0; i--) {
re = opt(n1[i], n2[i]) + re;
}
System.out.println(re);
String re2 = "";
for (int i = len - 1; i >= 0; i--) {
re2 = opt2(n1[i], n2[i]) + re2;
}
System.out.println(re2);
}
static byte opt(byte c1, byte c2) {
byte r = (byte) (c1 + c2 + temp);
temp = (byte) (r / 10);
return (byte) (r % 10);
}
static byte opt2(byte c1, byte c2) {
if ((c1 + temp2) >= c2) {
return (byte) (c1 + temp2 - c2);
} else {
byte b = (byte) (10 + c1 + temp2 - c2);
temp2 = -1;
return b;
}
}
}
Cals算法
最新推荐文章于 2024-11-06 15:05:29 发布
