Java - 查找具有最大总和的行和列

正如标题所说,我想知道一种方法(在 Java 中)找到哪一行(在矩阵/二维数组中)和哪一列的数字总和最大。可能有一个简单的解决方案,但我很难找到它。


我目前有程序的第一部分,但我似乎无法找到第二部分的解决方案,即找到总和最高的行和列。


http://img2.mukewang.com/6229b34b0001420303570180.jpg

我是这方面的初学者,所以任何形式的建议都将不胜感激。


这是我的代码的第一部分:


import javax.swing.JOptionPane;


public class summat{

    public static void main(String[] args){

        int mat[][] = new int [3][3];

        int num, sumop, sumw, i, j, mayop = 0, mayw = 0;


        for(i=0;i<3;i++){

            for(j=0;j<3;j++){

                String input = JOptionPane.showInputDialog(null, "Products sold by the operator " +  (i+1) + " in week " + (j+1) + ".");

                mat[i][j] = Integer.parseInt(input);

            }

        }


        /*Sum of individual rows*/

        for(i=0;i<3;i++){

            sumop = 0;

            for(j=0;j<3;j++){

                sumop = sumop + mat[i][j];

            }

            JOptionPane.showMessageDialog(null, "The operator " + (i+1) + " sold " + sumop + " units.");

        }


        /*Sum of individual columns*/

        for(j=0;j<3;j++){

            sumw = 0;

            for(i=0;i<3;i++){

                sumw = sumw + mat[i][j];

            }

            JOptionPane.showMessageDialog(null, "In week " + (j+1) + " the company sold " + sumw + " units.");

        }


    }

}


绝地无双
浏览 231回答 3
3回答

慕虎7371278

public static void method(int[] arr, int row, int col) {&nbsp; &nbsp; // converting array to matrix&nbsp; &nbsp; int index = 0;&nbsp; &nbsp; int mat[][] = new int[row][col];&nbsp; &nbsp; for (int i = 0; i < row; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < col; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mat[i][j] = arr[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // calculating sum of each row and adding to arraylist&nbsp; &nbsp; ArrayList<Integer> rsum = new ArrayList<Integer>();&nbsp; &nbsp; for (int i = 0; i < row; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int r = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < col; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = r + mat[i][j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; rsum.add(r);&nbsp; &nbsp; }&nbsp; &nbsp; // calculating sum of each col and adding to arraylist&nbsp; &nbsp; ArrayList<Integer> csum = new ArrayList<Integer>();&nbsp; &nbsp; for (int i = 0; i < row; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < col; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + mat[j][i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; csum.add(sum);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Maximum row sum is " + Collections.max(rsum) + " at row " + rsum.indexOf(Collections.max(rsum)));&nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Maximum col sum is " + Collections.max(csum) + " at col " + csum.indexOf(Collections.max(csum)));&nbsp; &nbsp;}public static void method(int[][] mat, int row, int col) {&nbsp; &nbsp; // calculating sum of each row and adding to arraylist&nbsp; &nbsp; ArrayList<Integer> rsum = new ArrayList<Integer>();&nbsp; &nbsp; for (int i = 0; i < row; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int r = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < col; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = r + mat[i][j];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; rsum.add(r);&nbsp; &nbsp; }&nbsp; &nbsp; // calculating sum of each col and adding to arraylist&nbsp; &nbsp; ArrayList<Integer> csum = new ArrayList<Integer>();&nbsp; &nbsp; for (int i = 0; i < row; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < col; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + mat[j][i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; csum.add(sum);&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Maximum row sum is " + Collections.max(rsum) + " at row " + rsum.indexOf(Collections.max(rsum)));&nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Maximum col sum is " + Collections.max(csum) + " at col " + csum.indexOf(Collections.max(csum)));}

RISEBY

您可以使用以下逻辑并根据需要实现它。&nbsp; &nbsp; // Row calculation&nbsp; &nbsp; int rowSum = 0, maxRowSum = Integer.MIN_VALUE, maxRowIndex = Integer.MIN_VALUE;&nbsp; &nbsp; for (int i = 0; i < 3; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 3; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowSum = rowSum + mat[i][j];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (maxRowSum < rowSum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxRowSum = rowSum;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxRowIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; rowSum = 0;&nbsp; &nbsp;// resetting before next iteration&nbsp; &nbsp; }&nbsp; &nbsp; // Column calculation&nbsp; &nbsp; int colSum = 0, maxColSum =&nbsp; Integer.MIN_VALUE, maxColIndex = Integer.MIN_VALUE;&nbsp; &nbsp; for (int i = 0; i < 3; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 3; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colSum = colSum + mat[j][i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (maxColSum < colSum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxColSum = colSum;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxColIndex = i;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; colSum = 0;&nbsp; &nbsp; // resetting before next iteration&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("Row " + maxRowIndex + " has highest sum = " +maxRowSum);&nbsp; &nbsp; System.out.println("Col " + maxColIndex + " has highest sum = " +maxColSum);这里我们使用两个额外的变量maxRowSum来存储行的最高和并maxRowIndex存储最高行的索引。这同样适用于列。

慕妹3146593

这是一种方法,它首先在一个循环中计算逐行和逐列总和(与用于查找最大行总和的方法相同),然后使用第二个方法来查找最大列总和://This returns an array with {maxRowIndex, maxColumnIndex}public static int[] findMax(int[][] mat) {&nbsp; &nbsp; int[] rowSums = new int[mat.length];&nbsp; &nbsp; int[] colSums = new int[mat[0].length];&nbsp; &nbsp; int maxRowValue = Integer.MIN_VALUE;&nbsp; &nbsp; int maxRowIndex = -1;&nbsp; &nbsp; for (int i = 0; i < mat.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < mat[i].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowSums[i] += mat[i][j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colSums[j] += mat[i][j];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (rowSums[i] > maxRowValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxRowIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxRowValue = rowSums[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // display current row message&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "The operator " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (i + 1) + " sold " + rowSums[i] + " units.");&nbsp; &nbsp; }&nbsp; &nbsp; int maxColumnValue = Integer.MIN_VALUE;&nbsp; &nbsp; int maxColumnIndex = -1;&nbsp; &nbsp; // look for max column:&nbsp; &nbsp; for (int j = 0; j < mat[0].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (colSums[j] > maxColumnValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxColumnValue = colSums[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxColumnIndex = j;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // display column message&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "In week " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; (j + 1) + " the company sold " + colSums[j] + " units.");&nbsp; &nbsp; }&nbsp; &nbsp; return new int[] { maxRowIndex, maxColumnIndex };}以下测试(我必须对矩阵值进行硬编码)产生 [2, 2]:public static void main(String[] args) {&nbsp; &nbsp; int mat[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };&nbsp; &nbsp; int[] maxValues = findMax(mat);&nbsp; &nbsp; System.out.println("Max row index: " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;maxValues[0] + ". Max Column index: " + maxValues[1]);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java