如何在Java中实现Big Integer减法?

时间:2016-11-16 13:49:47

标签: java algorithm biginteger subtraction

我想减去2个大整数并且不使用像BigInteger这样的API,并且我自己实现sum运算。

我已经实现了,如下所示,我可以添加2个不使用BigInteger的大整数。

class BigIntAdd{
    public static void main(String args[]){
        String s1 = "7654729850328997631007285998163550104";
        String s2 = "5980139243970186632651869926335829102";
        add(s1, s2);
    }

    public static String add(String addend1, String addend2) {
        StringBuilder buf = new StringBuilder();
        for ( int i1 = addend1.length() - 1, i2 = addend2.length() - 1, carry = 0;
              i1 >= 0 || i2 >= 0 || carry != 0;
              i1--, i2-- ) {
            int digit1 = i1 < 0 ? 0 :
                         Integer.parseInt(Character.toString(addend1.charAt(i1)));
            int digit2 = i2 < 0 ? 0 :
                         Integer.parseInt(Character.toString(addend2.charAt(i2)));

            int digit = digit1 + digit2 + carry;
            if (digit > 9) {
                carry = 1;
                digit -= 10;
            } else {
                carry = 0;
            }

            buf.append(digit);
        }
        return buf.reverse().toString();
    }

如何修改代码以便减去 2个整数?

1 个答案:

答案 0 :(得分:2)

这是一个减去BigInteger的简单例子。提前抱歉,因为我使用字符数组代替String来表示数字,但您可以理解。

private static char[] subtractValue(char[] greater, char[] smaller) {
        char[] result = new char[greater.length];
        int index = 1;
        int carry = 0;
        int digit;

        // 1. subtract shorter part
        while (index <= smaller.length) {
                digit = Character.getNumericValue(greater[greater.length-index]) - Character.getNumericValue(smaller[smaller.length-index]) - carry;
                carry = digit < 0 ? 1 : 0;
                digit = digit + (carry == 1? 10 : 0);
                result[greater.length - index] = Character.forDigit(digit, 10);
                index++;
        }

        // 2. carry rippling
        while (index <= greater.length) {
                digit = Character.getNumericValue(greater[greater.length-index]) - carry;
                carry = digit < 0 ? 1 : 0;
                digit = digit + (carry == 1 ? 10 : 0);
                result[greater.length - index] = Character.forDigit(digit, 10);
                index++;
        }

        // 3. trim out trailing zeros
        int i;
        for(i = 0; i < result.length - 1 && result[i] == '0'; i++) {
        }
        return Arrays.copyOfRange(result, i, result.length);
}

您可以这样调用它:

char[] bigger = new char[]{'7', '7', '8'};
char[] smaller = new char[]{'1', '2', '3'};
String result = Arrays.toString(subtractValue(bigger, smaller));
System.out.println(result);

希望它有所帮助!

修改

1。减去较短的部分

此部分代码只是从i中减去smaller的每个greater数字,如果有任何进位,它将传播(i + 1)数字的进位。

 2 1 0
 1 2 3
 -----
 0 8 7

2。携带涟漪

此部分减去先前步骤是否有任何进位,并在需要时传播新进位,直到进位变为零。

In (1) part, we did - 
2 0 0
0 7 8
------
0 2 2

Now, we have still carry 1 left from (1) to subtract

2 0 0
1
-----
1 2 2

3。修剪尾随零

此部分修剪掉尾部零。

1 2 3
1 2 1
-----
0 0 2

After trimming zeros

2