C语言结构体:【输出形式】标题名Id、Name、Score1、Score2、Score3、Score4、GPA用\t分隔; 一行中的数据也用\t分隔; 【输入/输出样例】红色部分代表输入的信息。 Please enter students number:5 Please input all student information: 101 s01 88 68 76 80 102 s02 78 80 88 69 103 s03 50 68 62 55 104 s04 90 99 92 95 105 s05 62 71 80 75 Id Name Score1 Score2 Score3 Score4 gpa 101 s01 88.00 68.00 76.00 80.00 11.20 102 s02 78.00 80.00 88.00 69.00 11.50 103 s03 50.00 68.00 62.00 55.00 3.00 104 s04 90.00 99.00 92.00 95.00 17.60 105 s05 62.00 71.00 80.00 75.00 8.80 The highest gpa is 17.60, Id is 104, name is s04.
时间: 2023-01-31 12:08:22 浏览: 172
如果要在 C 语言中输出上述信息,可以使用如下代码:
```
#include <stdio.h>
typedef struct {
int id;
char name[32];
float score1;
float score2;
float score3;
float score4;
float gpa;
} student;
int main(void) {
int n;
printf("Please enter students number: ");
scanf("%d", &n);
student students[n];
printf("Please input all student information:\n");
for (int i = 0; i < n; i++) {
scanf("%d %s %f %f %f %f", &students[i].id, students[i].name, &students[i].score1, &students[i].score2, &students[i].score3, &students[i].score4);
students[i].gpa = (students[i].score1 + students[i].score2 + students[i].score3 + students[i].score4) / 4;
}
printf("Id\tName\tScore1\tScore2\tScore3\tScore4\tgpa\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].id, students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].score4, students[i].gpa);
}
float maxGpa = students[0].gpa;
int maxGpaIndex = 0;
for (int i = 1; i < n; i++) {
if (students[i].gpa > maxGpa) {
maxGpa = students[i].gpa;
maxGpaIndex = i;
}
}
printf("The highest gpa is %.2f, Id is %d, name is %s.\n", maxGpa, students[maxGpaIndex].id, students[maxGpaIndex].name);
return 0;
}
```
在这段代码中,我们首先读入了学生的数量 `n`,然后利用结构体 `student` 存储学生的信息。接着,我们读入每个学生的信息并计算出他们的平均分(GPA)。最后,我们遍历整个学生数组,找出 GPA 最高的学生并
阅读全文