C++编写程序求出二维数组中的鞍点。
时间: 2024-06-14 11:03:40 浏览: 91
编写程序,找出一个二维数组的鞍点,即在当前行最大,当前列最小的元素,也可能没有鞍点。
```cpp
#include <iostream>
using namespace std;
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
bool hasSaddlePoint = false;
for (int i = 0; i < 3; i++) {
int maxInRow = matrix[i][0];
int colIndex = 0;
for (int j = 1; j < 3; j++) {
if (matrix[i][j] > maxInRow) {
maxInRow = matrix[i][j];
colIndex = j;
}
}
bool isSaddlePoint = true;
for (int k = 0; k < 3; k++) {
if (matrix[k][colIndex] < maxInRow) {
isSaddlePoint = false;
break;
}
}
if (isSaddlePoint) {
cout << "Saddle point found at position (" << i << "," << colIndex << ") with value " << maxInRow << endl;
hasSaddlePoint = true;
}
}
if (!hasSaddlePoint) {
cout << "No saddle point found in the matrix." << endl;
}
return 0;
}
```
阅读全文