我试图为像BigInteger这样的类实现一个add函数,其中输入是一个疯狂的长数字。但是,当我从字节数组转换为字符串时,屏幕上没有任何内容正在打印。但是我在main函数中注释掉的示例代码可以正常工作。有谁知道为什么?
以下是coderpad上的输出
resut字节数组:[7,9,1,0]
100 + 9 =
import java.io.*;
import java.util.*;
import java.nio.charset.Charset;
class Solution {
private static Charset charset = Charset.forName("UTF-8");
public static class BigInteger {
public byte[] digits;
public BigInteger(String input) {
digits = input.getBytes(charset);
}
public BigInteger(byte[] digits) {
this.digits = digits;
// System.out.println(Arrays.toString(digits));
}
public String add(BigInteger other) {
byte[] digits1, digits2;
if (this.digits.length >= other.digits.length) {
digits1 = this.digits;
digits2 = other.digits;
} else {
digits1 = other.digits;
digits2 = this.digits;
}
int remainder = 0;
int s1 = digits1.length - 1;
int s2 = digits2.length - 1;
byte[] resultDigits = new byte[s1 + 2];
for (int i = s1; i >= 0; i--) {
int digit1 = digits1[i];
int digit2 = i > s2 ? 0 : digits2[i];
int r = digit1 + digit2 + remainder;
remainder = r > 9 ? 1 : 0;
resultDigits[i] = (byte) (r % 10);
// System.out.println(resultDigits[i]);
}
resultDigits[s1] = (byte) remainder;
// System.out.println("resut byte array: " + Arrays.toString(resultDigits));
String result = new String(resultDigits, charset);
return result;
}
}
public static void main(String[] args) {
BigInteger a = new BigInteger("100");
BigInteger b = new BigInteger("9");
System.out.println("100 + 9 = " + a.add(b));
// String abc="123";
// byte[] ba = abc.getBytes();
// System.out.println(Arrays.toString(ba));
// System.out.println(new String(ba, Charset.forName("UTF-8")));
}
}
---编辑 - add()v2
public String add(BigInteger other) {
byte[] digits1, digits2;
if (this.digits.length >= other.digits.length) {
digits1 = this.digits;
digits2 = other.digits;
} else {
digits1 = other.digits;
digits2 = this.digits;
}
int remainder = 0;
int s1 = digits1.length - 1;
int s2 = digits2.length - 1;
byte[] resultDigits = new byte[s1 + 2];
for (int i = s1; i >= 0; i--) {
int digit1 = digits1[s1--];
int digit2 = s2 >= 0 ? digits2[s2--] : 0;
int sum = digit1 + digit2 + remainder;
remainder = sum / 10;
resultDigits[i] = (byte) (sum % 10);
// System.out.println(resultDigits[i]);
}
resultDigits[0] = (byte) remainder; // probably dont need this
System.out.println("resut byte array: " + Arrays.toString(resultDigits));
String result = new String(resultDigits, charset);
System.out.println("result as string: " + result);
return result;
}
答案 0 :(得分:1)
以下是更正:
public String add(BigInteger other) {
byte[] digits1, digits2;
if (this.digits.length >= other.digits.length) {
digits1 = this.digits;
digits2 = other.digits;
} else {
digits1 = other.digits;
digits2 = this.digits;
}
int remainder = 0;
int s1 = digits1.length - 1;
int s2 = digits2.length - 1;
byte[] resultDigits = new byte[s1 + 2];
for (int i = s1; i >= 0; i--) {
int diff = digits1.length - digits2.length;
int digit1 = digits1[i] - 48;
int digit2 = i < diff ? 0 : digits2[i - diff] - 48;
int r = digit1 + digit2 + remainder;
remainder = r > 9 ? 1 : 0;
resultDigits[i+1] = (byte)(r % 10 + 48);
}
resultDigits[0] += (byte) remainder + 48;
String result = resultDigits[0] == 48 ? new String(resultDigits).substring(1) : new String(resultDigits);
return result;
}
基本上,错误是:
输出:
0 + 45 = 45
19 + 95 = 114
38 + 20 = 58
57 + 92 = 149
76 + 67 = 143
95 + 54 = 149
114 + 90 = 204
133 + 48 = 181
152 + 6 = 158
171 + 18 = 189
190 + 22 = 212
209 + 13 = 222
228 + 32 = 260
247 + 24 = 271