酷代码 AI
菜单
服务商

C++实现矩阵中马鞍数的查找与位置输出

```cpp #include <iostream> #include <vector> using namespace std; int main() { int n, m; // 输入矩阵的行数和列数 cin >> n >> m; // 定义并初始化矩阵 vector<vector<int>> matrix(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> matrix[i][j]; } } bool found = false; // 遍历矩阵的每一行 for (int i = 0; i < n; ++i) { int maxInRow = matrix[i][0]; int maxColIndex = 0; // 找出当前行的最大值及其列索引 for (int j = 1; j < m; ++j) { if (matrix[i][j] > maxInRow) { maxInRow = matrix[i][j]; maxColIndex = j; } } bool isMinInCol = true; // 检查该最大值是否是所在列的最小值 for (int k = 0; k < n; ++k) { if (matrix[k][maxColIndex] < maxInRow) { isMinInCol = false; break; } } // 如果是所在列的最小值,输出其位置 if (isMinInCol) { cout << i + 1 << " " << maxColIndex + 1 << endl; found = true; } } // 如果没有找到马鞍数,输出No answer if (!found) { cout << "No answer" << endl; } return 0; } ``` ######[AI问答 | 442点数解答 | 2026-02-27 12:18:29]

相关提问
发财导航,免费问AI
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]