#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> scores;
int num_students, score, total_score = 0;
cout << "请输入学生数量:" << endl;
cin >> num_students;
for (int i = 1; i <= num_students; i++) {
cout << "请输入" << i << "号学生的成绩:" << endl;
cin >> score;
scores.push_back(score);
total_score += score;
}
double average_score = total_score / num_students;
int max_score = scores[0], min_score = scores[0];
for (int i = 1; i < num_students; i++) {
if (scores[i] > max_score) max_score = scores[i];
if (scores[i] < min_score) min_score = scores[i];
}
cout << "班级平均分为:" << average_score << endl;
cout << "最高分为:" << max_score << endl;
cout << "最低分为:" << min_score << endl;
return 0;
}
这个程序要求用户输入学生数量和每个学生的成绩,然后计算班级的平均分、最高分和最低分,并将这些信息输出到屏幕上。
程序首先使用一个vector来存储每个学生的成绩,使用push_back函数将每个成绩添加到向量中。然后,程序遍历vector来计算平均分、最高分和最低分。最后,程序将这些数据输出到屏幕上。
请注意,此程序假设用户输入的每个值都是有效的整数。如果输入的值不是整数,则程序可能会产生意外的结果或崩溃。在实际应用中,需要添加错误处理代码来确保输入的数据是有效的。