java寻找二维数组的鞍点
时间: 2023-12-31 08:24:33 浏览: 106
求一个二维数组的鞍点
以下是使用Java编程语言寻找二维数组鞍点的示例代码:
```java
public class SaddlePointFinder {
public static void main(String[] args) {
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = array.length;
int cols = array[0].length;
for (int i = 0; i < rows; i++) {
int minRowValue = array[i][0];
int minColIndex = 0;
// 找出当前行的最小值和对应的列索引
for (int j = 1; j < cols; j++) {
if (array[i][j] < minRowValue) {
minRowValue = array[i][j];
minColIndex = j;
}
}
// 检查当前列的最大值是否等于当前行的最小值
boolean isSaddlePoint = true;
for (int k = 0; k < rows; k++) {
if (array[k][minColIndex] > minRowValue) {
isSaddlePoint = false;
break;
}
}
// 如果是鞍点,则输出其值和位置
if (isSaddlePoint) {
System.out.println("Saddle point found at (" + i + ", " + minColIndex + "): " + minRowValue);
}
}
}
}
```
阅读全文