题目:某班共有若干名学生学生信息用如下的stuinfo结构表示 其中男学生则gender取值为’m’女学生则gender取值为’f’。 2022年春季学期4门课程的成绩依次保存在score1~score4中。 请编写函数int statisticsstuinfo stus int n 分别统计男生和女生的4门课程总成绩的最高分 其中stus数组存储了班上所有学生的信息
#include
struct stuinfo { int id; // 学号 char name[10]; // 姓名 char gender; // 性别 int score1; // 课程1成绩 int score2; // 课程2成绩 int score3; // 课程3成绩 int score4; // 课程4成绩 };
int statistics(stuinfo stus[], int n) { int maxMaleScore = 0; // 男生总成绩的最高分 int maxFemaleScore = 0; // 女生总成绩的最高分
for (int i = 0; i < n; i++) {
if (stus[i].gender == 'm') {
// 计算男生总成绩
int maleScore = stus[i].score1 + stus[i].score2 + stus[i].score3 + stus[i].score4;
if (maleScore > maxMaleScore) {
maxMaleScore = maleScore;
}
} else if (stus[i].gender == 'f') {
// 计算女生总成绩
int femaleScore = stus[i].score1 + stus[i].score2 + stus[i].score3 + stus[i].score4;
if (femaleScore > maxFemaleScore) {
maxFemaleScore = femaleScore;
}
}
}
return maxMaleScore - maxFemaleScore;
}
int main() { stuinfo stus[20]; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> stus[i].id >> stus[i].name >> stus[i].gender >> stus[i].score1 >> stus[i].score2 >> stus[i].score3 >> stus[i].score4; } cout << statistics(stus, n) << endl;
return 0;
原文地址: https://www.cveoy.top/t/topic/hJya 著作权归作者所有。请勿转载和采集!