在n行n列(n<=10)的二维整数数组中,按照以下要求选出一个数。首先从每行中选出最大数,再从选出的n个最大数中选出最小数. 程序运行示例: Please enter the order of the matrix:3 Please input the elements of the matrix: 9 2 3 4 5 8 7 2 1 The minimum of maximum number is 7. 输入提示:"Please enter the order of the matrix:" 输入格式:%d 输入提示:"Please input the elements of the matrix:\n" 输入格式:%d 输出提示:"The minimum of maximum number is %d.\n"
时间: 2024-03-02 13:50:14 浏览: 83
Number Triangles 给定一个由n行数字组成的数字三角形如下图所示。试设计一个算法,计算出从三角形的顶至底的一条路径,使该路径经过的数字总和最大。
4星 · 用户满意度95%
以下是实现该功能的 C++ 代码:
```cpp
#include <iostream>
using namespace std;
int main() {
int n, max_num, min_max_num;
cout << "Please enter the order of the matrix:";
cin >> n;
int matrix[n][n];
cout << "Please input the elements of the matrix:" << endl;
// 读入矩阵
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
// 找出每行最大值
int row_max[n];
for (int i = 0; i < n; i++) {
max_num = matrix[i][0];
for (int j = 1; j < n; j++) {
if (matrix[i][j] > max_num) {
max_num = matrix[i][j];
}
}
row_max[i] = max_num;
}
// 找出最大值中的最小值
min_max_num = row_max[0];
for (int i = 1; i < n; i++) {
if (row_max[i] < min_max_num) {
min_max_num = row_max[i];
}
}
cout << "The minimum of maximum number is " << min_max_num << "." << endl;
return 0;
}
```
输入示例:
```
Please enter the order of the matrix:3
Please input the elements of the matrix:
9 2 3
4 5 8
7 2 1
```
输出示例:
```
The minimum of maximum number is 7.
```
阅读全文