java找出一个二维数组的鞍点,即该位置上的元素在该行上最大、在列上最小(也可能没有鞍点。)
时间: 2023-11-06 14:08:29 浏览: 98
可以用以下代码来找出一个二维数组的鞍点:
```java
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int n = matrix.length;
int m = matrix[0].length;
for (int i = 0; i < n; i++) {
int rowMax = matrix[i][0];
int rowMaxIndex = 0;
for (int j = 1; j < m; j++) {
if (matrix[i][j] > rowMax) {
rowMax = matrix[i][j];
rowMaxIndex = j;
}
}
boolean isSaddlePoint = true;
for (int k = 0; k < n; k++) {
if (matrix[k][rowMaxIndex] < rowMax) {
isSaddlePoint = false;
break;
}
}
if (isSaddlePoint) {
System.out.println("Saddle point found at (" + i + ", " + rowMaxIndex + ")");
break;
}
}
System.out.println("There is no saddle point in this matrix.");
```
如果二维数组中存在鞍点,它的位置将被输出;否则将输出 "There is no saddle point in this matrix."。
要注意的是,这只是代码的示例,实际运行中可能还需要考虑一些边界情况和特殊情况。
阅读全文