C++不使用vector头文件完成:实验小学为了强健学生的身体每天课间都要组织学生在户外学做广播体操。这一天五年级三班的所有同学在老师的指引下将队形排成了M行N列。 现已知所有同学的身高数值为整数单位:厘米。要求在所有同学中:1挑选出每列身高最高的同学作为此列的小队长为同学们带操;2将所有同学按身高从高到低的顺序进行排队;3求出所有同学们的平均身高要求:四舍五入保留整数;4统计出不小于平均身高的学
以下是使用C++编程解决该问题的代码:
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main() {
int M, N;
cin >> M >> N;
int heights[M][N];
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
cin >> heights[i][j];
}
}
int max_heights[N];
for(int j = 0; j < N; j++) {
int max_height = heights[0][j];
for(int i = 1; i < M; i++) {
max_height = max(max_height, heights[i][j]);
}
max_heights[j] = max_height;
cout << max_height << endl;
}
int all_heights[M * N];
int index = 0;
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
all_heights[index] = heights[i][j];
index++;
}
}
sort(all_heights, all_heights + (M * N), greater<int>());
for(int i = 0; i < M * N; i++) {
cout << all_heights[i] << " ";
}
cout << endl;
int sum_height = 0;
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
sum_height += heights[i][j];
}
}
int average_height = round((double)sum_height / (M * N));
cout << average_height << endl;
int count = 0;
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
if(heights[i][j] >= average_height) {
count++;
}
}
}
cout << count << endl;
return 0;
}
希望能对你有所帮助
原文地址: http://www.cveoy.top/t/topic/hXE3 著作权归作者所有。请勿转载和采集!