如何使用Java找到矩阵的鞍点? [英] How to find a saddle point of a matrix using Java?

查看:286
本文介绍了如何使用Java找到矩阵的鞍点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I find a saddle point of a matrix, which is the highest number in the row and at the same time the highest number in column using Java?

例如,使用此矩阵:

| 7 2 |

| 1 3 |

| 5 8 |

马鞍点是:7和8.

这是我的代码的一部分我写道找到行和列中的最高数字。

Here is the portion of my code I wrote to find the highest number in the row and in the column.

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

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

Now I want to find the saddle point using the definition above.

推荐答案

不幸的是我得走了。看起来你最终会到达那里。
这是一个基于你的描述的解决方案,你描述它的方式应该有效。

Unfortunately I have to go. It looks like you were going to get there in the end. Here is a solution which is based on your description, and the way you describe it should work.

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

It's not the most efficient, you should improve that. You also should not submit this as an assignment, doing so would only be cheating yourself, you will find future assignments difficult.

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);
        }
    }
}

这篇关于如何使用Java找到矩阵的鞍点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆