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

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

查找哪個矩陣行具有最高和java

查找哪個矩陣行具有最高和java

臨摹微笑 2023-06-04 17:07:13
我正在嘗試編寫一個遞歸非靜態方法,該方法對于每個給定矩陣將返回具有最高總和的行號。我不允許使用靜態方法和 for 循環(在我編寫的任何方法中)。我認為解決方案是使用三種方法:(private) 計算給定行的總和。(私人)比較行的總和與使用方法#1i的總和i+1(公共)檢查矩陣是否有多于一行并在行上應用#2?0。我覺得我用我解決這個問題的方式把事情復雜化了。如果有人愿意向我建議更好的算法,我會很樂意嘗試。無論如何,我相信我對#1 和#3 沒問題。我的問題是#2。我不知道如何設置行號變量:public class Matrix {private int[][] _mat;public Matrix(int sizeRow, int sizeCol) {? ?_mat = new int[sizeRow][sizeCol];}private int maxRow(int row) { //Recursive method #2: comparing sum of i and i+1? ? ? ? int rowNumber;? ? ? ? if (row <= _mat.length) {? ? ? ? ? ? if (rowSum(row, 0) > rowSum(row+1,0)) {? ? ? ? ? ? ? ? rowNumber = row;? ? ? ? ? ? ? ? return maxRow(row+1);? ? ? ? ? ? }? ? ? ? ? ? else {? ? ? ? ? ? ? ? return rowNumber;? ? ? ? ? ? }? ? ? ? }? ? ? ? else {? ? ? ? ? ? return rowNumber;? ? ? ? }? ? }..public int maxRow() {..} //Recursive method #3private int rowSum(int i, int j) {..} //Recursive method #1}我的問題是 var?rowNumber。它還沒有被初始化,如果我要初始化它,它將被設置為0每次我調用該方法時。
查看完整描述

2 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

如果需要返回總和最大的行的索引,可以這樣做:


private int maxRow(int current_index, int max_index) {

    if (current_index == _mat.length) {

        return max_index;

    } else if (sumRow(current_index) > sumRow(max_index)) {

        return maxRow(current_index+1, current_index);

    } else {

        return maxRow(current_index+1, max_index);

    }

}


maxRow(1, 0); //method call

第一個參數current_index存儲您當前正在測試的索引,而參數max_index存儲到目前為止訪問過的具有最大總和的索引。


第一個子句確保您在到達數組末尾時返回具有最大總和值的任何索引。


max_index一旦找到總和高于之前的行,第二個子句就會更新。


當上述情況沒有發生時,第三個子句只是迭代到下一行。


您可以調用該方法current_index=1,max_index=0因此您不需要max_index使用無效值進行初始化。


如果你想提高性能,你還可以添加一個新的參數max_value來存儲當前的最大和,這樣你就不需要在每次遞歸調用時都調用它。


查看完整回答
反對 回復 2023-06-04
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

這是一個示例,說明如何檢索總和最高的行的索引。


public class MaxRowFromArray {


    private final int[][] values;


    public MaxRowFromArray(int[][] values) {

        this.values = values;

    }


    private int sumOfRow(int[] row, int rowIndex, int sum) {

        if (rowIndex > row.length - 1) {

            return sum;

        }

        return sumOfRow(row, rowIndex + 1, sum + row[rowIndex]);

    }


    private int highestRow(int column, int highestIndex, int highestRow) {

        if (column > values.length - 1) {

            return highestIndex;

        }

        int sumOfRow = sumOfRow(values[column], 0, 0);


        if (sumOfRow > highestRow) {

            return highestRow(column + 1, column, sumOfRow);

        }

        return highestRow(column + 1, highestIndex, highestRow);

    }


    public int highestRow() {

        int highest = highestRow(0, 0, -1);


        if (highest == -1) {

            throw new IllegalStateException("No row can be found with the highest sum.");

        }

        return highest;

    }

}

測試


    public static void main(String[] args) {

        MaxRowFromArray max = new MaxRowFromArray(new int[][] {

                { 1 },

                { 1, 2 },

                { 1, 2, 3 },

                { 1, 2, 3, 4}

        });


        int expectedHighest = 3;


        int highestIndex = max.highestRow();


        if (highestIndex != expectedHighest) {

            throw new AssertionError(String.format("Highest index %s was not the expected highest %s.",

                    highestIndex, expectedHighest));

        }

        System.out.println("Highest: " + highestIndex);

    }


查看完整回答
反對 回復 2023-06-04
  • 2 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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