某学习小组有3个人,每个人的信息包括:学号、姓名和成绩。要求从键盘上输入他们的信息,并求出平均成绩以及最高成绩者的信息。
说明:
1.学生结构体类型
typedef struct Student {
int num;
char name[20];
int score;
}STU;
2.平均值保留2位小数;
3.输出最高成绩者信息时各项之间用“\t”隔开。
测试用例:输入 | 测试用例:输出 |
1< 回车 > hangsan< 回车 > 86< 回车 > 2< 回车 > lisi< 回车 > 84< 回车 > 3< 回车 > wanger< 回车 > 93< 回车 >
| The average score=87.67 The student who has the highest score is: 3 wanger 93
|
代码如下:
#include <stdio.h>
typedef struct Student {
int num;
char name[20];
int score;
}STU;
int main()
{
STU student[3];
int i, max = 0;
double average = 0.0;
for (i = 0; i < 3; ++i) {
scanf("%d%s%d", &student[i].num, student[i].name, &student[i].score);
average += student[i].score;
if (student[i].score >= student[max].score) {
max = i;
}
}
average /= 3;
printf("The average score=%.2lf\n", average);
printf("The student who has the highest score is:\n");
printf("%d\t%s\t%d", student[max].num, student[max].name, student[max].score);
return 0;
}