考研真题c语言

【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;
}
```

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

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值