查找数组中行的绝对值,然后打印出答案的最大值

Finding the absolute value of rows in array and then print the maximum value out ot the answer

我目前需要仅使用绝对值将数组中的所有行相加,然后从三个答案中找出最大值。

这是我目前得到的:

public static int maxRowAbsSum(int[][] array) {
    int[][] maxRowValue = {

                            {3, -1,  4,  0},
                            {5,  9, -2,  6},
                            {5,  3,  7, -8}

                       };
    int maxRow = 0;
    int indexofMaxRow = 0;
        for (int column = 0; column < maxRowValue[0].length; column++) {
            maxRow += maxRowValue[0][column];
            }

            for (int row = 1; row < maxRowValue.length; row++) {
                int totalOfRow = 0;
                for (int column = 0; column < maxRowValue[row].length; column++){
                    totalOfRow += maxRowValue[row][column];

                    if (totalOfRow > maxRow) {
                        maxRow = totalOfRow;
                        indexofMaxRow = row;
                    }
                }

                System.out.println("Row " + indexofMaxRow + " has the sum of " + maxRow);
            }
            return indexofMaxRow;
        }

但这只是 returns 索引第 1 行的总数,没有使用绝对值。

更新:我将如何编写 JUnit 代码来使用 assertArrayEquals 对此进行测试?

访问完该行的所有元素就应该求和,单独计算第一行是多余的:

int maxRow = 0;
int indexofMaxRow = 0;

for (int row = 0; row < maxRowValue.length; row++) {
    int totalOfRow = 0;
    for (int column = 0; column < maxRowValue[row].length; column++){
         if (maxRowValue[row][column] > 0) {
             totalOfRow += maxRowValue[row][column];
         } else {
             totalOfRow -= maxRowValue[row][column];
         }
     }
     if (totalOfRow > maxRow) {
         maxRow = totalOfRow;
         indexofMaxRow = row;
     }
}
System.out.println("Row " + indexofMaxRow + " has the sum of " + maxRow);
return indexofMaxRow;

您的代码是正确的。只是一个小改动。

1) 因为你想要绝对值,所以使用Math.abs()函数来添加值。

2) 行:System.out.println("Row " + indexofMaxRow + " has the sum of " + maxRow);这将显示 CURRENT MAXIMUM ROW 的索引。仅当下一行的值更大时,索引才会更改。第 3 行的总和小于第 2 行。因此在这种情况下索引不会更新。所以你在两个循环中得到相同的输出!这个例子可以证明:{3, -1, 4, 0}, {5, 9, -2, 6}, {5, 3, 7, 9} 输出:第 1 行的总和为 18 第 2 行的总和为24 2

的总和
        int[][] arr = {
            {3, -1,  4,  0},
            {5,  9, -2,  6},
            {5,  3,  7, -8}
       };

    int maxSum = 0;
    int i = 0;
    int tempSum = 0;

    for(int j=0; j<arr[0].length; j++) {
        //System.out.println("i: " + i + " j:" + j);
        tempSum += Math.abs(arr[i][j]);

        //if j is checking last element of the row, then go to next row, set tempSum to 0
        if(j==arr[0].length-1) {
            j=-1; i++; //j is set to -1 because it will get incremented to 0 because of for loop
            System.out.println("Sum of row: " + i + ": " + tempSum);
            if(tempSum > maxSum) maxSum = tempSum;
            tempSum=0;
        }

        //check if i completed all rows
        if(i == arr[0].length-1) {
            break;
        }
    }

    System.out.println("Max sum: " + maxSum);