请问“有 10个学生,每个学生的数据包括学号、姓名、3 门课程的成绩,从键盘输人10个学生数据,要求输出 3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3 门课程成绩、平均分数)”这道题用C语言如何写出来? 如果可以一题多解的话,请写出所有答案,谢谢。
时间: 2023-12-15 11:45:04 浏览: 34
好的,以下是两种用C语言解决这个问题的方法:
方法一:
```c
#include <stdio.h>
#include <string.h>
#define NUM_STUDENTS 10
#define NUM_COURSES 3
struct Student {
char id[10];
char name[20];
int scores[NUM_COURSES];
};
int main() {
struct Student students[NUM_STUDENTS];
int total_scores[NUM_COURSES] = {0};
float max_average_score = 0.0;
struct Student max_student;
// 输入学生信息
printf("请输入10个学生的信息:\n");
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("请输入第%d个学生的学号、姓名、三门课程的成绩,中间用空格隔开:\n", i+1);
scanf("%s %s %d %d %d", students[i].id, students[i].name, &students[i].scores[0],
&students[i].scores[1], &students[i].scores[2]);
// 计算总成绩
for (int j = 0; j < NUM_COURSES; j++) {
total_scores[j] += students[i].scores[j];
}
// 计算平均成绩
float average_score = (students[i].scores[0] + students[i].scores[1] + students[i].scores[2]) / 3.0;
// 更新最高平均成绩的学生信息
if (average_score > max_average_score) {
max_average_score = average_score;
strcpy(max_student.id, students[i].id);
strcpy(max_student.name, students[i].name);
max_student.scores[0] = students[i].scores[0];
max_student.scores[1] = students[i].scores[1];
max_student.scores[2] = students[i].scores[2];
}
}
// 计算平均成绩
float average_scores[NUM_COURSES];
for (int i = 0; i < NUM_COURSES; i++) {
average_scores[i] = total_scores[i] / (float)NUM_STUDENTS;
}
// 输出结果
printf("三门课程的总平均成绩为:%.2f %.2f %.2f\n", average_scores[0], average_scores[1], average_scores[2]);
printf("最高分的学生信息为:\n");
printf("学号:%s\n", max_student.id);
printf("姓名:%s\n", max_student.name);
printf("三门课程成绩:%d %d %d\n", max_student.scores[0], max_student.scores[1], max_student.scores[2]);
printf("平均分数:%.2f\n", max_average_score);
return 0;
}
```
方法二:
```c
#include <stdio.h>
#include <string.h>
#define NUM_STUDENTS 10
#define NUM_COURSES 3
struct Student {
char id[10];
char name[20];
int scores[NUM_COURSES];
};
int main() {
struct Student students[NUM_STUDENTS];
int total_scores[NUM_COURSES] = {0};
float max_average_score = 0.0;
int max_student_index = 0;
// 输入学生信息
printf("请输入10个学生的信息:\n");
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("请输入第%d个学生的学号、姓名、三门课程的成绩,中间用空格隔开:\n", i+1);
scanf("%s %s %d %d %d", students[i].id, students[i].name, &students[i].scores[0],
&students[i].scores[1], &students[i].scores[2]);
// 计算总成绩
for (int j = 0; j < NUM_COURSES; j++) {
total_scores[j] += students[i].scores[j];
}
}
// 计算平均成绩
float average_scores[NUM_COURSES];
for (int i = 0; i < NUM_COURSES; i++) {
average_scores[i] = total_scores[i] / (float)NUM_STUDENTS;
}
// 找出最高平均成绩的学生
for (int i = 0; i < NUM_STUDENTS; i++) {
float average_score = (students[i].scores[0] + students[i].scores[1] + students[i].scores[2]) / 3.0;
if (average_score > max_average_score) {
max_average_score = average_score;
max_student_index = i;
}
}
// 输出结果
printf("三门课程的总平均成绩为:%.2f %.2f %.2f\n", average_scores[0], average_scores[1], average_scores[2]);
printf("最高分的学生信息为:\n");
printf("学号:%s\n", students[max_student_index].id);
printf("姓名:%s\n", students[max_student_index].name);
printf("三门课程成绩:%d %d %d\n", students[max_student_index].scores[0],
students[max_student_index].scores[1], students[max_student_index].scores[2]);
printf("平均分数:%.2f\n", max_average_score);
return 0;
}
```
以上两种方法分别采用了不同的思路来解决这个问题,其中方法一在输入学生信息的同时,就计算出了每个学生的平均成绩,然后再找出最高平均成绩的学生;而方法二先输入所有学生的信息,然后再遍历所有学生,计算出每个学生的平均成绩,最后找出最高平均成绩的学生。虽然实现的思路不同,但都能得出正确的结果。