Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                    
                
        

如何使用Java找到矩阵的鞍点,该鞍点是行中的最高数字,同时列中的最高数字?

例如,使用此矩阵:

| 7 2 |

| 1 3 |

| 5 8 |

鞍点为:7和8。

这是我为在行和列中找到最高编号而编写的代码部分。

int NumRow = 3;
int NumCol = 2;
int [] matrix = new int [NumRow][NumCol];

for ( int i = 0; i < NumRow; i++)
{
    max = matrix[i][0];

    for ( int j = 1; j < NumCol; j++)
    {
        if (matrix[i][j]> max)
        {
            max = matrix[i][j];
        }
    }
    System.out.print(max+"  ");

}
System.out.println("\n");

for ( int c = 0; c < NumCol; c++)
{
    largest = matrix[c][0];

    for (int r = 0; r < NumRow; r++){
        if (matrix[r][c] > largest){
            largest = matrix[r][c];
        }
    }
    System.out.print(largest+"  ");
}


输出为:

7 3 8

7 8

现在,我想使用上面的定义找到鞍点。

最佳答案

不幸的是我必须走。看来您最终要到达那里。
这是一个基于您的描述的解决方案,并且您描述它的方式应该起作用。

它不是最有效的,您应该改进它。您也不应将此作业作为作业提交,这样做只会欺骗自己,您会发现以后的作业很困难。

public class SaddlePointerFinder{

    public static void main(String[] args){

       int [] [] matrix = {
        { 7, 2 },
        { 1, 3 },
        { 5, 8 },
       };

       // i loops though columns
       for(int x = 0; x<matrix[0].length; x++){

       // this is to store the highest on the ROW
        int highestOnTheRow = -1;
       // this is to store the index of that highest value
       int indexOfHighest = -1;

       // x loops through rows
       for(int y = 0; y<matrix.length; y++){
            if(matrix[y][x] > highestOnTheRow) {
                // update the highest
                highestOnTheRow = matrix[y][x];
                indexOfHighest = y;
            }
        }

        // After checking the whole row and finding the highest, check if it's highest on the column
        boolean highest = true;

        // here, i checks goes through each row using that column.
        for(int i = 0; i<matrix[0].length; i++){
            if(matrix[indexOfHighest][i] > highestOnTheRow) {
                // one which was higher was found :(
                highest = false;
            }
        }
        if(highest){
               System.out.println("If the forumla is correct, this is a saddle point: " + highestOnTheRow);
        }
    }
}

10-08 14:38