首页 > 解决方案 > 如何在不影响数组中其他值的情况下操作可变 NaturalNumber 数组?

问题描述

下面是带有代码的合约。基本上,如果 的值为,则应nnArray为。问题是当 被调用时,整个数组将根据. 所以如果是那么将是[1,2,3,4,5,6]neww[1,2,6,24,,120,720]neww[i].multiply(c);cc2nnArray[2,2,2,2,2,2]

/*
* Replaces each element of {@code nnArray} with the partial product of all
     * the elements in the incoming array, up to and including the current
     element.


     *
     * @param nnArray
     *            the array
     * @updates nnArray
     * @requires nnArray.length > 0
     * @ensures <pre>
     * for all i: integer where (0 <= i < nnArray.length)
     *   (nnArray[i] = [#nnArray[0] * #nnArray[1] * ... * #nnArray[i]])
     * </pre>
     */
    private static void computePartialProducts(NaturalNumber[] nnArray) {
        assert nnArray != null : "Violation of: nnArray is not null";
        assert nnArray.length > 0 : "Violation of: nnArray.length > 0";

        NaturalNumber[] neww = new NaturalNumber[nnArray.length];

        NaturalNumber count = new NaturalNumber2(1);
        for (int i = 0; i < nnArray.length; i++) {

            neww[i] = count;

        }

        for (int i = 0; i < nnArray.length; i++) {

            NaturalNumber c = new NaturalNumber2(nnArray[i]);

            // neww[i] = c;

            neww[i].multiply(c);

        }

标签: java

解决方案


推荐阅读