Java 比基特减法
我在用Java中的carrys进行减法时遇到了一个问题Java 比基特减法,java,biginteger,Java,Biginteger,我在用Java中的carrys进行减法时遇到了一个问题 public BigInt add(BigInt o) { int carry = 0; int max = n.length > o.n.length ? n.length : o.n.length; int[] result = new int[max+1]; for (int i = 0; i <= max; ++i) { int top = i < n.length
public BigInt add(BigInt o) {
int carry = 0;
int max = n.length > o.n.length ? n.length : o.n.length;
int[] result = new int[max+1];
for (int i = 0; i <= max; ++i) {
int top = i < n.length ? n[i] : 0;
int bot = i < o.n.length ? o.n[i] : 0;
result[i] = (top + bot + carry) % 10;
carry = (top + bot + carry) / 10;
}
return new BigInt(trim(result));
}
public BigInt sub(BigInt o) {
int carry = 0;
int max = n.length > o.n.length ? n.length : o.n.length;
int[] result = new int[max+1];
for (int i = 0; i <= max; ++i) {
int top = i < n.length ? n[i] : 0;
int bot = i < o.n.length ? o.n[i] : 0;
carry = (top + bot + carry) / 10;
result[i] = (10 + top - bot - carry) % 10;
}
return new BigInt(trim(result));
}
公共BigInt添加(BigInt o){
整数进位=0;
int max=n.length>o.n.length?n.length:o.n.length;
int[]结果=新的int[max+1];
对于(int i=0;i o.n.length?n.length:o.n.length;
int[]结果=新的int[max+1];
对于(int i=0;i来说,您的代码有很多错误,但首先要做的是提供所需的结果:
public BigInt sub(BigInt o)
{
int carry = 0;
int max = n.length > o.n.length ? n.length : o.n.length;
int[] result = new int[max+1];
for (int i = 0; i <= max; ++i)
{
int top = i < n.length ? n[i] : 0;
int bot = i < o.n.length ? o.n[i] : 0;
int res = top - bot - carry;
result[i] = (10 + res) % 10;
carry = res < 0 ? 1 : 0;
}
return new BigInt(trim(result));
}
public BigInt sub(BigInt o)
{
整数进位=0;
int max=n.length>o.n.length?n.length:o.n.length;
int[]结果=新的int[max+1];
对于(int i=0;i