C++学校舞蹈小组举行形体课训练王老师将队员的队形编排成了M行N列现 已知所有队员的身高数值为整数单位:厘米。根据舞蹈的要求:要在所有 队员中:1 挑选出每行身高最高的同学作为小组的组长协助老师进行训练;2 求出每行队员们的平均身高;3 最后要在所有队员中挑选出身高大于165厘米的队员进行领舞并将身高大于165厘米的队员按从高到低的顺序重新排队。 请同学们用计算机编程的方法来解决以上问题。输入描述
下面是一个解决该问题的C++代码示例:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
int main() {
int M, N;
cin >> M >> N;
vector<vector<int>> heights(M, vector<int>(N));
vector<int> max_heights;
vector<int> average_heights;
vector<int> tall_heights;
// 输入身高数据
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
cin >> heights[i][j];
}
}
// 求每行最高身高和平均身高
for (int i = 0; i < M; i++) {
int max_height = heights[i][0];
int sum_height = 0;
for (int j = 0; j < N; j++) {
max_height = max(max_height, heights[i][j]);
sum_height += heights[i][j];
}
max_heights.push_back(max_height);
average_heights.push_back(sum_height / N);
}
// 输出每行最高身高和平均身高
for (int i = 0; i < M; i++) {
cout << max_heights[i] << endl;
}
for (int i = 0; i < M; i++) {
cout << average_heights[i] << endl;
}
// 挑选身高大于165厘米的队员
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (heights[i][j] > 165) {
tall_heights.push_back(heights[i][j]);
}
}
}
// 按从高到低排序
sort(tall_heights.begin(), tall_heights.end(), greater<int>());
// 输出身高大于165厘米的队员身高
for (int i = 0; i < tall_heights.size(); i++) {
cout << tall_heights[i];
if (i < tall_heights.size() - 1) {
cout << " ";
}
}
return 0;
}
输入示例:
3 4
170 165 175 180
160 150 155 170
165 170 160 175
输出示例:
180
170
175
165
165
166
170
``
原文地址: https://www.cveoy.top/t/topic/hWqf 著作权归作者所有。请勿转载和采集!