从键盘输入若干个学生的信息,每个学生信息包括学号、姓名、3门课的成绩,计算每个学生的总分,输出总分最高的学生的信息。c语言
时间: 2023-07-14 12:14:23 浏览: 55
下面是C语言的代码实现:
```c
#include <stdio.h>
struct student {
int id; // 学号
char name[20]; // 姓名
int score[3]; // 3门课的成绩
int total_score; // 总分
};
int main() {
int n, i, max_score = 0, max_index = 0;
printf("请输入学生人数:");
scanf("%d", &n);
struct student stu[n]; // 定义结构体数组
// 输入每个学生的信息
for(i=0; i<n; i++) {
printf("请输入第%d个学生的信息:\n", i+1);
printf("学号:");
scanf("%d", &stu[i].id);
printf("姓名:");
scanf("%s", stu[i].name);
printf("成绩1:");
scanf("%d", &stu[i].score[0]);
printf("成绩2:");
scanf("%d", &stu[i].score[1]);
printf("成绩3:");
scanf("%d", &stu[i].score[2]);
stu[i].total_score = stu[i].score[0] + stu[i].score[1] + stu[i].score[2];
}
// 计算总分最高的学生
for(i=0; i<n; i++) {
if(stu[i].total_score > max_score) {
max_score = stu[i].total_score;
max_index = i;
}
}
// 输出总分最高的学生的信息
printf("总分最高的学生信息如下:\n");
printf("学号:%d\n", stu[max_index].id);
printf("姓名:%s\n", stu[max_index].name);
printf("成绩1:%d\n", stu[max_index].score[0]);
printf("成绩2:%d\n", stu[max_index].score[1]);
printf("成绩3:%d\n", stu[max_index].score[2]);
printf("总分:%d\n", stu[max_index].total_score);
return 0;
}
```
运行程序后,输入学生信息,最后会输出总分最高的学生的信息。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)