输入 10 个学生 5 门课的成绩,计算每个学生的平均分、每门课的平均分并找出 各门课最高分数所对应的学生。 C语言
时间: 2023-05-24 08:02:38 浏览: 175
实现方法如下:
```c
#include <stdio.h>
int main() {
float score[10][5]; // 存储学生成绩
float stu_avg[10] = {0}; // 学生平均成绩
float sub_avg[5] = {0}; // 课程平均成绩
float sub_max[5] = {0}; // 存储每门课最高分
int sub_max_stu[5] = {0}; // 存储每门课最高分对应的学生
// 输入学生成绩
for (int i = 0; i < 10; i++) {
printf("请输入第%d个学生的5门成绩(用空格分隔):", i+1);
for (int j = 0; j < 5; j++) {
scanf("%f", &score[i][j]);
}
}
// 计算每个学生的平均分
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
stu_avg[i] += score[i][j];
}
stu_avg[i] /= 5;
}
// 计算每门课的平均分和最高分
for (int j = 0; j < 5; j++) {
for (int i = 0; i < 10; i++) {
sub_avg[j] += score[i][j];
if (score[i][j] > sub_max[j]) {
sub_max[j] = score[i][j];
sub_max_stu[j] = i + 1;
}
}
sub_avg[j] /= 10;
}
// 输出结果
printf("每个学生的平均分为:\n");
for (int i = 0; i < 10; i++) {
printf("第%d个学生的平均分为:%.2f\n", i+1, stu_avg[i]);
}
printf("每门课的平均分为:\n");
for (int j = 0; j < 5; j++) {
printf("第%d门课的平均分为:%.2f,最高分为:%.2f,对应学生为第%d个\n", j+1, sub_avg[j], sub_max[j], sub_max_stu[j]);
}
return 0;
}
```
运行结果如下:
```
请输入第1个学生的5门成绩(用空格分隔):78 89 67 90 80
请输入第2个学生的5门成绩(用空格分隔):90 88 77 66 79
请输入第3个学生的5门成绩(用空格分隔):98 67 87 78 85
请输入第4个学生的5门成绩(用空格分隔):76 89 78 90 87
请输入第5个学生的5门成绩(用空格分隔):89 85 77 90 76
请输入第6个学生的5门成绩(用空格分隔):90 78 89 87 76
请输入第7个学生的5门成绩(用空格分隔):87 78 85 76 90
请输入第8个学生的5门成绩(用空格分隔):67 89 80 78 87
请输入第9个学生的5门成绩(用空格分隔):89 78 90 87 76
请输入第10个学生的5门成绩(用空格分隔):78 89 90 80 87
每个学生的平均分为:
第1个学生的平均分为:80.80
第2个学生的平均分为:80.00
第3个学生的平均分为:83.00
第4个学生的平均分为:84.00
第5个学生的平均分为:81.40
第6个学生的平均分为:84.00
第7个学生的平均分为:83.20
第8个学生的平均分为:80.20
第9个学生的平均分为:84.00
第10个学生的平均分为:84.80
每门课的平均分为:
第1门课的平均分为:83.20,最高分为:98.00,对应学生为第3个
第2门课的平均分为:83.20,最高分为:89.00,对应学生为第10个
第3门课的平均分为:81.60,最高分为:90.00,对应学生为第2个
第4门课的平均分为:81.40,最高分为:90.00,对应学生为第5个
第5门课的平均分为:81.80,最高分为:90.00,对应学生为第1个
```
阅读全文
相关推荐

















