自定义BigInteger add(3,2)返回{5,0}

时间:2016-11-07 22:09:43

标签: biginteger

我试图用.add方法做一个自定义BigInteger类。 这是我的代码:

public BigNumber plus(BigNumber other) {
        int[] resultPointer = new int[Math.max(digits.length, other.digits.length) + 1];
        int carry = 0;
        for (int i = 0; i < resultPointer.length - 1; i++) {
            int left = digits.length > i ? digits[i] : 0;
            int right = other.digits.length > i ? other.digits[i] : 0;

            int result = left + right + carry;
            carry = result >= 10 ? result - 9 : 0;
            resultPointer[i] = result <= 9 ? result : 9;
        }

        return new BigNumber(resultPointer);
    }

并得出它的BigNumber(我的biginteger名称类为50,具体为int {5,0})

0 个答案:

没有答案