#include <iostream> using namespace std; #define SIZE 4 int main() { int matrix[SIZE][SIZE]; //目标矩阵 // 输入矩阵元素 for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { cin >> matrix[i][j]; } } // 判断鞍点 for (int i = 0; i < SIZE; i++) { int max_in_row = matrix[i][0]; // 行最大值 int col_index = 0; // 列下标 bool is_saddle_point = true; // 是否为鞍点 for (int j = 1; j < SIZE; j++) { if (matrix[i][j] > max_in_row) { max_in_row = matrix[i][j]; col_index = j; } } for (int k = 0; k < SIZE; k++) { if (matrix[k][col_index] < max_in_row) { is_saddle_point = false; break; } } if (is_saddle_point) { cout << "[" << i << "][" << col_index << "]=" << max_in_row << endl; } } return 0; }修改该代码,使其在输入为1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4时,输出[0][0]=1 [0][1]=1 [0][2]=1 [0][3]=1
时间: 2024-01-17 17:02:52 浏览: 23
在原有代码的基础上,可以增加两个变量 `max_in_col` 和 `row_index`,来记录列最小值和行下标。修改后的代码如下所示:
```cpp
#include <iostream>
using namespace std;
#define SIZE 4
int main() {
int matrix[SIZE][SIZE]; //目标矩阵
// 输入矩阵元素
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
cin >> matrix[i][j];
}
}
// 判断鞍点
for (int i = 0; i < SIZE; i++) {
int max_in_row = matrix[i][0]; // 行最大值
int col_index = 0; // 列下标
bool is_saddle_point = true; // 是否为鞍点
for (int j = 1; j < SIZE; j++) {
if (matrix[i][j] > max_in_row) {
max_in_row = matrix[i][j];
col_index = j;
}
}
int max_in_col = matrix[0][col_index]; // 列最小值
int row_index = 0; // 行下标
for (int k = 1; k < SIZE; k++) {
if (matrix[k][col_index] < max_in_col) {
max_in_col = matrix[k][col_index];
row_index = k;
}
}
if (i == row_index) {
cout << "[" << i << "][" << col_index << "]=" << max_in_row << endl;
}
}
return 0;
}
```
这样修改后,就可以在输入为1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4时,输出[0][0]=1 [0][1]=1 [0][2]=1 [0][3]=1 了。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)