编写程序,将输入的20名学生的成绩保存到数组中,求出其最高分、最低分及平均分。 编写程序,将输入的20名学生的成绩保存到数组中,求出其最高分、最低分及平均分。 输入格式: 11 12 23 36 48 56 72 78 89 96 78 89 90 100 78 89 56 67 45 98 输出格式: max=100.0 min=11.0 avg=65.6
时间: 2024-01-24 21:19:03 浏览: 48
以下是Python代码实现:
```python
scores = input().split() # 输入成绩,以空格分隔,并转换为字符串列表
scores = list(map(float, scores)) # 将字符串列表转换为浮点数列表
max_score = max(scores) # 求最高分
min_score = min(scores) # 求最低分
avg_score = sum(scores) / len(scores) # 求平均分
print("max={:.1f} min={:.1f} avg={:.1f}".format(max_score, min_score, avg_score)) # 输出结果
```
使用`input()`函数获取输入,将输入的字符串以空格分割并转换为浮点数列表。使用Python内置的`max`和`min`函数求得最高分和最低分,使用`sum`函数求和并除以列表长度求得平均分。最后使用`print`函数输出结果,使用字符串格式化将结果格式化为指定格式输出。
相关问题
编写程序,将输入的20名学生的成绩保存到数组中,求出其最高分,最低分,平均分
好的,这是一个简单的程序,可以实现你的需求:
```python
scores = []
for i in range(20):
score = int(input("请输入第{}名学生的成绩:".format(i+1)))
scores.append(score)
max_score = max(scores)
min_score = min(scores)
avg_score = sum(scores) / len(scores)
print("最高分:{}".format(max_score))
print("最低分:{}".format(min_score))
print("平均分:{}".format(avg_score))
```
请注意,这个程序只是一个示例,实际应用中可能需要更多的输入验证和错误处理。
编写程序,将输入的20名学生的成绩保存到数组中,求出其最高分、最低分及平均分C语言代码
#include <stdio.h>
int main() {
int scores[20];
int sum = 0;
int max = 0;
int min = 100;
// 输入成绩
for (int i = 0; i < 20; i++) {
printf("请输入第%d个学生的成绩:", i+1);
scanf("%d", &scores[i]);
// 求和
sum += scores[i];
// 求最高分
if (scores[i] > max) {
max = scores[i];
}
// 求最低分
if (scores[i] < min) {
min = scores[i];
}
}
// 求平均分
float average = (float)sum / 20.0;
// 输出结果
printf("最高分:%d\n", max);
printf("最低分:%d\n", min);
printf("平均分:%f\n", average);
return 0;
}