java 使用 byte[] 进行大整数加法
java biginteger addition using byte[]
我正在尝试为像 BigInteger 这样的 class 实现一个加法函数,其中输入是一个非常长的数字。但是,当我从字节数组转换为字符串时,屏幕上没有打印任何内容。但是我在主要功能中注释掉的示例代码有效。有谁知道为什么?
这是我在 coderpad
上的输出
结果字节数组:[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")));
}
}
--- 编辑 --
添加() 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;
}
此处更正:
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;
}
基本上,错误是:
- 你并没有从你的值中减去 48,因为你知道你正在输入字符
- 您还遇到了较小操作数的位置问题。
- 最后,你最后加的余数加错了位置。您应该始终将其添加到您的第一个索引中。
输出:
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
我正在尝试为像 BigInteger 这样的 class 实现一个加法函数,其中输入是一个非常长的数字。但是,当我从字节数组转换为字符串时,屏幕上没有打印任何内容。但是我在主要功能中注释掉的示例代码有效。有谁知道为什么?
这是我在 coderpad
上的输出
结果字节数组:[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")));
}
}
--- 编辑 -- 添加() 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;
}
此处更正:
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;
}
基本上,错误是:
- 你并没有从你的值中减去 48,因为你知道你正在输入字符
- 您还遇到了较小操作数的位置问题。
- 最后,你最后加的余数加错了位置。您应该始终将其添加到您的第一个索引中。
输出:
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