java 找二维数组鞍点
时间: 2023-05-11 14:02:24 浏览: 81
求一个二维数组的鞍点
可以使用以下代码来找到二维数组的鞍点:
```java
public static void findSaddlePoint(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
boolean saddlePointFound = false;
for (int i = 0; i < rows; i++) {
int minRow = matrix[i][0];
int colIndex = 0;
for (int j = 1; j < cols; j++) {
if (matrix[i][j] < minRow) {
minRow = matrix[i][j];
colIndex = j;
}
}
boolean isSaddlePoint = true;
for (int k = 0; k < rows; k++) {
if (matrix[k][colIndex] > minRow) {
isSaddlePoint = false;
break;
}
}
if (isSaddlePoint) {
System.out.println("Saddle point found at (" + i + ", " + colIndex + ")");
saddlePointFound = true;
}
}
if (!saddlePointFound) {
System.out.println("No saddle point found");
}
}
```
这个方法会遍历每一行,找到每一行的最小值,然后检查该最小值是否是该列的最大值。如果是,则找到了一个鞍点。如果没有找到任何鞍点,则输出“没有找到鞍点”。
阅读全文