簡體   English   中英

具有兩個不同整數對象數組的 add 方法

[英]add method with two different array of integer objects

我正在嘗試在一個類中實現一個方法,該方法為被調用對象和其他對象表示的整數總和創建並返回一個新對象。

例如

BigInt val1 = new BigInt(1111111);
BigInt val2 = new BigInt(2222);
BigInt sum = val1.add(val2);
System.out.println(sum);
should print
1113333

我的 add 方法似乎工作正常,但是當它到達最后一個索引時,它不會將 1 帶到最后一個索引。 這是我的代碼和測試用例。

public BigInt add(BigInt other) {
//        int length = this.digits.length < other.digits.length ? this.digits.length : other.digits.length;
        BigInt added = new BigInt();
        int MAX_numsig = this.numSigDigits < other.numSigDigits ? other.getNumSigDigits() : this.getNumSigDigits();

        added.numSigDigits = MAX_numsig;


        if (other == null) {
            throw new IllegalArgumentException("parameter is null");
        }
        if (this.getNumSigDigits() >= SIZE || other.getNumSigDigits() >= SIZE) {
            throw new ArithmeticException("Sum size bigger than final static value SIZE");
        }
        int carry = 0;
        for (int i = this.digits.length - 1; i >= 0; i--) {


            if (this.digits[i] + other.digits[i] + carry < 10) {
                added.digits[i] = this.digits[i] + other.digits[i] + carry;
                carry = 0;

            }
            if (this.digits[i] + other.digits[i] + carry >= 10) {
                added.digits[i] = (this.digits[i] + other.digits[i] + carry) % 10;

                if (i == SIZE - MAX_numsig) {
                    added.numSigDigits = MAX_numsig + 1;
                }
                if (i == 0) {
                    throw new ArithmeticException("overflow");
                } else {
                    carry = 1;
                }
            }

        }



        return added;

    }

這是測試用例:

 System.out.println("Test 12: result should be\n123456789123456789");
        int[] a4 = {3, 6, 1, 8, 2, 7, 3, 6, 0, 3, 6, 1, 8, 2, 7, 3, 6};
        int[] a5 = {8, 7, 2, 7, 4, 0, 5, 3, 0, 8, 7, 2, 7, 4, 0, 5, 3};
        BigInt b4 = new BigInt(a4);
        BigInt b5 = new BigInt(a5);
        BigInt sum2 = b4.add(b5);
        System.out.println(sum2);
        System.out.println();

這個測試用例應該返回 123456789123456789。相反,它返回 23456789123456789。我不明白為什么 1 沒有通過最后一個索引。

SIZE 是 20 的默認靜態最終值。誰能幫我解決一個錯誤?

原因是兩個數字之和的位數可以大於兩個數字,例如在您給定的示例中,和的位數為 18,兩個值的位數均為 17。但您的代碼不是考慮到這部分。

因此,如果和中的位數較大,則循環存在后將有一個進位值。 您應該將進位值添加到結果中。

希望能幫助到你。 如果您不理解任何部分,請告訴我。

這是一種方法。 可能有更有效的方法,但它強調了一些需要解決的問題。

        String a = "9999";
        String b = "999";

        // pad smallest on left with 0's.
        int diff = a.length() - b.length();
        if (diff < 0) {
            a  = "0".repeat(-diff) + a;
        } else {
            b = "0".repeat(diff) + b;
        }

        // convert both string to arrays of int digits.
        int[] aa = a.chars().map(c -> c - '0').toArray();
        int[] bb = b.chars().map(c -> c - '0').toArray();

        int carry = 0;
        // iterate over the array and add and process the carry bit
        for (int i = aa.length-1; i >= 0; i--) {
            bb[i] += (aa[i] + carry);
            carry = 0;
            if (bb[i] > 9) {
                bb[i] %= 10;
                carry = 1;
            }
        }

        // Now convert the array of digits to a numeric string.
        String result =
                Arrays.stream(bb).mapToObj(c -> (char)(c + '0') + "")
                        .collect(Collectors.joining());

        // there could still be a carry to process. e.g "999" + "1"
        if (carry == 1) {
            result = '1' + result;
        }
        System.out.println(result);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM