为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

绝地无双 2022-03-10 16:13:42
正如标题所说,我想知道一种方法(在 Java 中)找到哪一行(在矩阵/二维数组中)和哪一列的数字总和最大。可能有一个简单的解决方案,但我很难找到它。我目前有程序的第一部分,但我似乎无法找到第二部分的解决方案,即找到总和最高的行和列。我是这方面的初学者,所以任何形式的建议都将不胜感激。这是我的代码的第一部分: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.");        }    }}
查看完整描述

3 回答

?
慕虎7371278

TA贡献1802条经验 获得超4个赞

public static void method(int[] arr, int row, int col) {

    // converting array to matrix

    int index = 0;

    int mat[][] = new int[row][col];

    for (int i = 0; i < row; i++) {

        for (int j = 0; j < col; j++) {

            mat[i][j] = arr[index];

            index++;

        }

    }

    // calculating sum of each row and adding to arraylist

    ArrayList<Integer> rsum = new ArrayList<Integer>();

    for (int i = 0; i < row; i++) {

        int r = 0;

            for (int j = 0; j < col; j++) {

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

            }

        rsum.add(r);

    }

    // calculating sum of each col and adding to arraylist

    ArrayList<Integer> csum = new ArrayList<Integer>();

    for (int i = 0; i < row; i++) {

        int sum = 0;

        for (int j = 0; j < col; j++) {

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

            }

        csum.add(sum);

        }

    System.out.println(

            "Maximum row sum is " + Collections.max(rsum) + " at row " + rsum.indexOf(Collections.max(rsum)));

    System.out.println(

            "Maximum col sum is " + Collections.max(csum) + " at col " + csum.indexOf(Collections.max(csum)));   

}


public static void method(int[][] mat, int row, int col) {


    // calculating sum of each row and adding to arraylist

    ArrayList<Integer> rsum = new ArrayList<Integer>();

    for (int i = 0; i < row; i++) {

        int r = 0;

        for (int j = 0; j < col; j++) {

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

        }

        rsum.add(r);

    }

    // calculating sum of each col and adding to arraylist

    ArrayList<Integer> csum = new ArrayList<Integer>();

    for (int i = 0; i < row; i++) {

        int sum = 0;

        for (int j = 0; j < col; j++) {

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

        }

        csum.add(sum);

    }

    System.out.println(

            "Maximum row sum is " + Collections.max(rsum) + " at row " + rsum.indexOf(Collections.max(rsum)));

    System.out.println(

            "Maximum col sum is " + Collections.max(csum) + " at col " + csum.indexOf(Collections.max(csum)));

}


查看完整回答
反对 回复 2022-03-10
?
RISEBY

TA贡献1856条经验 获得超5个赞

您可以使用以下逻辑并根据需要实现它。


    // Row calculation

    int rowSum = 0, maxRowSum = Integer.MIN_VALUE, maxRowIndex = Integer.MIN_VALUE;

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

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

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

        }

        if (maxRowSum < rowSum) {

            maxRowSum = rowSum;

            maxRowIndex = i;

        }

        rowSum = 0;   // resetting before next iteration

    }


    // Column calculation

    int colSum = 0, maxColSum =  Integer.MIN_VALUE, maxColIndex = Integer.MIN_VALUE;

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

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

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

        }

        if (maxColSum < colSum) {

            maxColSum = colSum;

            maxColIndex = i; 

        }

        colSum = 0;    // resetting before next iteration

    }


    System.out.println("Row " + maxRowIndex + " has highest sum = " +maxRowSum);

    System.out.println("Col " + maxColIndex + " has highest sum = " +maxColSum);

这里我们使用两个额外的变量maxRowSum来存储行的最高和并maxRowIndex存储最高行的索引。这同样适用于列。


查看完整回答
反对 回复 2022-03-10
?
慕妹3146593

TA贡献1820条经验 获得超9个赞

这是一种方法,它首先在一个循环中计算逐行和逐列总和(与用于查找最大行总和的方法相同),然后使用第二个方法来查找最大列总和:


//This returns an array with {maxRowIndex, maxColumnIndex}

public static int[] findMax(int[][] mat) {

    int[] rowSums = new int[mat.length];

    int[] colSums = new int[mat[0].length];


    int maxRowValue = Integer.MIN_VALUE;

    int maxRowIndex = -1;


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

        for (int j = 0; j < mat[i].length; j++) {

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

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

        }


        if (rowSums[i] > maxRowValue) {

            maxRowIndex = i;

            maxRowValue = rowSums[i];

        }


        // display current row message

        JOptionPane.showMessageDialog(null, "The operator " +

                (i + 1) + " sold " + rowSums[i] + " units.");

    }


    int maxColumnValue = Integer.MIN_VALUE;

    int maxColumnIndex = -1;


    // look for max column:

    for (int j = 0; j < mat[0].length; j++) {

        if (colSums[j] > maxColumnValue) {

            maxColumnValue = colSums[j];

            maxColumnIndex = j;

        }


        // display column message

        JOptionPane.showMessageDialog(null, "In week " + 

        (j + 1) + " the company sold " + colSums[j] + " units.");

    }


    return new int[] { maxRowIndex, maxColumnIndex };

}

以下测试(我必须对矩阵值进行硬编码)产生 [2, 2]:


public static void main(String[] args) {

    int mat[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };


    int[] maxValues = findMax(mat);

    System.out.println("Max row index: " + 

       maxValues[0] + ". Max Column index: " + maxValues[1]);

}


查看完整回答
反对 回复 2022-03-10
  • 3 回答
  • 0 关注
  • 230 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号