有5个学生,每个学生的数据信息包括学号、姓名、ABC三门课程的成绩,c语言要求打印出三门课程总分最高的学生的信息。
时间: 2023-06-19 16:10:30 浏览: 73
可以使用结构体来存储学生的数据信息,然后遍历结构体数组,计算每个学生的三门课程总分,最后找到总分最高的学生输出其信息。
以下是一个简单的实现:
```c
#include <stdio.h>
#define NUM_STUDENTS 5
struct student {
int id;
char name[20];
int scoreA;
int scoreB;
int scoreC;
};
int main() {
struct student students[NUM_STUDENTS] = {
{ 1, "Alice", 80, 90, 85 },
{ 2, "Bob", 75, 85, 90 },
{ 3, "Charlie", 90, 80, 95 },
{ 4, "David", 85, 95, 80 },
{ 5, "Eve", 95, 85, 90 }
};
int max_total = -1;
int max_index = -1;
for (int i = 0; i < NUM_STUDENTS; i++) {
int total = students[i].scoreA + students[i].scoreB + students[i].scoreC;
if (total > max_total) {
max_total = total;
max_index = i;
}
}
printf("Student with highest total score:\n");
printf("ID: %d\n", students[max_index].id);
printf("Name: %s\n", students[max_index].name);
printf("Score A: %d\n", students[max_index].scoreA);
printf("Score B: %d\n", students[max_index].scoreB);
printf("Score C: %d\n", students[max_index].scoreC);
printf("Total score: %d\n", max_total);
return 0;
}
```
输出结果为:
```
Student with highest total score:
ID: 3
Name: Charlie
Score A: 90
Score B: 80
Score C: 95
Total score: 265
```