【2016年山西大学考研真题】输入10个学生三门课的成绩,用函数实现:找出最高的分数所对应的学号和成绩。


1. 定义一个结构体 `Student` 来表示每个学生,包括学号和三门课的成绩。

“`c
typedef struct {
int studentID;
int score1;
int score2;
int score3;
} Student;
“`

2. 编写一个函数 `findMaxScore`,该函数通过遍历学生数组来找到最高分数对应的学号和成绩。

“`c
void findMaxScore(Student students[], int n, int* maxStudentID, int* maxScore) {
*maxScore = -1; // 初始化最高分数为一个较小的值
for (int i = 0; i < n; i++) {
int maxOfThree = (students[i].score1 > students[i].score2) ? students[i].score1 : students[i].score2;
maxOfThree = (maxOfThree > students[i].score3) ? maxOfThree : students[i].score3;
if (maxOfThree > *maxScore) {
*maxScore = maxOfThree;
*maxStudentID = students[i].studentID;
}
}
}
“`

3. 在 `main` 函数中创建一个包含10个学生的数组,并调用 `findMaxScore` 函数来找到最高分数的学号和成绩。

“`c
int main() {
Student students[10] = {
{1, 80, 85, 90},
{2, 75, 70, 95},
// 填写剩余学生的信息
};

int maxStudentID, maxScore;
findMaxScore(students, 10, &maxStudentID, &maxScore);

printf(“最高分数对应的学号:%d\n”, maxStudentID);
printf(“最高分数:%d\n”, maxScore);

return 0;
}
“`

在上述代码中,我们通过遍历学生数组,比较每个学生的三门课的最高分数,找到最高分数并记录其对应的学号。最后,我们输出最高分数对应的学号和成绩。