亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號