学生成绩问题。有10个学生,每个学生的数据包括学号、姓名、3门课的成绩。从键盘输入10个学生数据,要求输出每门课的总平均成绩及最高分学生的数据(用结构体数组实现)。
时间: 2023-12-15 11:24:10 浏览: 85
下面是一份 C++ 代码实现:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string id;
string name;
double scores[3];
};
int main() {
const int n = 10;
Student students[n];
double total_scores[3] = {0}; // 记录每门课的总成绩
int max_score_student_index = 0; // 记录最高分学生的下标
// 输入学生数据
for (int i = 0; i < n; i++) {
cout << "请输入第" << i+1 << "个学生的学号、姓名、3门课的成绩:" << endl;
cin >> students[i].id >> students[i].name;
for (int j = 0; j < 3; j++) {
cin >> students[i].scores[j];
total_scores[j] += students[i].scores[j];
if (students[i].scores[j] > students[max_score_student_index].scores[j]) {
max_score_student_index = i;
}
}
}
// 计算平均成绩
double avg_scores[3];
for (int i = 0; i < 3; i++) {
avg_scores[i] = total_scores[i] / n;
}
// 输出结果
cout << "每门课的总平均成绩为:" << endl;
cout << "语文:" << avg_scores[0] << endl;
cout << "数学:" << avg_scores[1] << endl;
cout << "英语:" << avg_scores[2] << endl;
cout << "最高分学生的数据为:" << endl;
cout << "学号:" << students[max_score_student_index].id << endl;
cout << "姓名:" << students[max_score_student_index].name << endl;
cout << "语文成绩:" << students[max_score_student_index].scores[0] << endl;
cout << "数学成绩:" << students[max_score_student_index].scores[1] << endl;
cout << "英语成绩:" << students[max_score_student_index].scores[2] << endl;
return 0;
}
该程序使用结构体数组存储学生信息,通过循环输入和计算平均成绩,再通过遍历计算最高分学生的下标,最后输出结果。
相关推荐


















