输入5个学生的三门成绩求每一科平均成绩和每一位学生的平均成绩
时间: 2023-09-07 17:11:05 浏览: 150
好的,这是一个统计问题,您可以先输入每个学生的三门成绩,然后计算每一科的平均成绩和每一位学生的平均成绩。以下是示例代码:
```python
scores = []
for i in range(5):
print("请输入第%d位学生的三门成绩:" % (i+1))
score1, score2, score3 = map(int, input().split())
scores.append([score1, score2, score3])
# 计算每一科的平均成绩
avg_scores = [sum(i)/5 for i in zip(*scores)]
print("每一科的平均成绩分别为:%.2f, %.2f, %.2f" % (avg_scores[0], avg_scores[1], avg_scores[2]))
# 计算每一位学生的平均成绩
avg_student_scores = [sum(i)/3 for i in scores]
print("每一位学生的平均成绩分别为:%.2f, %.2f, %.2f, %.2f, %.2f" % tuple(avg_student_scores))
```
当然,实际使用时还需要考虑异常情况等细节,这里只是提供一个简单的示例。
相关问题
用c语言求一个学生的三门成绩的平均值,要求成绩输入值可以改变
在C语言中,我们可以创建一个函数来计算学生的三门成绩的平均值。首先,需要定义一个结构体来存储学生的姓名和分数,然后编写一个函数来获取用户输入并计算平均分。下面是一个简单的示例:
```c
#include <stdio.h>
#include <string.h>
// 定义学生信息结构体
typedef struct {
char name[50];
int score1;
int score2;
int score3;
} Student;
// 函数用于计算平均分
float calculate_average(Student student) {
float sum = student.score1 + student.score2 + student.score3;
return sum / 3.0; // 注意这里除以3.0,避免整数除法导致丢失小数部分
}
int main() {
Student stu;
printf("请输入学生的姓名:\n");
fgets(stu.name, sizeof(stu.name), stdin); // 使用fgets读取字符串,包括空格
stu.name[strlen(stu.name) - 1] = '\0'; // 去掉多余的换行符
printf("请输入学生的三门成绩(用逗号隔开):\n");
scanf("%d,%d,%d", &stu.score1, &stu.score2, &stu.score3);
float average = calculate_average(stu);
printf("学生的平均成绩为: %.2f\n", average); // 输出保留两位小数
return 0;
}
```
在这个程序中,用户可以随时更改输入的成绩值,通过修改`scanf`函数中的数字即可。注意,这个例子假设用户会按照要求输入数字。
使用c语言数组输入三个学生三门课程的成绩,分别求每个学生的平均成绩和每门课程的平均成绩
在C语言中,你可以通过定义一个结构体来存储学生的姓名和三门课程的成绩,然后使用循环遍历数组来计算平均成绩。这里是一个简单的示例:
```c
#include <stdio.h>
#include <string.h>
// 定义学生结构体
typedef struct {
char name[50];
int score[3]; // 存储三门课程成绩
} Student;
int main() {
Student students[3]; // 存放三个学生信息的数组
// 输入学生信息
for (int i = 0; i < 3; i++) {
printf("请输入第 %d 位学生的姓名:", i + 1);
fgets(students[i].name, sizeof(students[i].name), stdin); // 去掉换行符
for (int j = 0; j < 3; j++) {
printf("请输入第%d门课程的成绩(%d-%d分):", j + 1, students[i].score[j - 1] + 1, 100); // 提示范围
scanf("%d", &students[i].score[j]);
}
}
// 计算每个学生的平均成绩
float student_averages[3] = {0};
for (int i = 0; i < 3; i++) {
float sum = 0;
for (int j = 0; j < 3; j++) {
sum += students[i].score[j];
}
student_averages[i] = sum / 3;
printf("学生 %s 的平均成绩是 %.2f\n", students[i].name, student_averages[i]);
}
// 计算每门课程的平均成绩
float course_averages[3] = {0};
for (int j = 0; j < 3; j++) {
float total = 0;
for (int i = 0; i < 3; i++) {
total += students[i].score[j];
}
course_averages[j] = total / 3;
printf("第%d门课程的平均成绩是 %.2f\n", j + 1, course_averages[j]);
}
return 0;
}
阅读全文
相关推荐















