(4)有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩、平均分数)。(所有人3科的总平均成绩,3门课平均的最高分)码尽量简洁易懂。
#include <stdio.h>
struct Student {
char xuehao[32];
char name[128];
float score1;
float score2;
float score3;
};
int main(void)
{
struct Student max; // 保存最高分学生信息
float score = 0.0; // 保存最高分学生分数
float total = 0.0; // 所有学生分数
for (int i = 0; i < 10; ++i) {
struct Student tmp;
scanf("%s %s %f %f %f", &tmp.xuehao, &tmp.name, &tmp.score1, &tmp.score2, &tmp.score3);
// 判断是否最高分
if (tmp.score1 + tmp.score2 + tmp.score3 - score > 0.000001) {
score = tmp.score1 + tmp.score2 + tmp.score3;
max = tmp;
}
// 累加总分
total += tmp.score1 + tmp.score2 + tmp.score3;
}
printf("3门课总平均分为: %.2f\n", total / (10 * 3));
printf("最高分学生信息: 学号[%s], 姓名[%s], 成绩[%.2f, %.2f, %.2f], 平均分数[%.2f]\n", max.xuehao, max.name, max.score1, max.score2, max.score3, score / 3);
return 0;
}
#include <stdio.h>
int main()
{
char a[10][6];
int i,index = 0,maxAverage = 0;
// 录入
for(i=0;i<10;i++){
scanf("%s %s %lf %lf %lf",&a[i][0],&a[i][1],&a[i][2],&a[i][3],&a[i][4]);
}
// 计算每个同学的平均成绩
for(i=0;i<10;i++){
float ave = (a[i][2] + a[i][3] + a[i][4]) / 3;
a[i][5] = ave;
printf("%s的平均成绩:%lf\n",a[i][0],ave);
}
// 找最大分,也就是平局分最高
maxAverage= a[0][5]
for(i=0;i<10;i++){
if(a[i][5] > maxAverage){
maxAverage= a[i][5];
index = i;
}
}
// 输出最高分的学生的数据
printf("%s %s %lf %lf %lf %lf\n",a[index][0],&a[index][1],&a[index][2],&a[index][3],&a[index][4],&a[index][5]);
return 0;
}