青年歌手参加歌曲大奖赛,有10个评委对她进行打分,试编程求这位选手的平均得分(去掉一个最高分和一个最低分)
时间: 2023-06-17 19:07:07 浏览: 87
假设这位选手的得分为一个长度为10的列表 `scores`,可以按照以下步骤计算平均得分:
1. 使用 Python 内置函数 `sorted()` 对 `scores` 列表进行排序,得到一个从小到大排列的新列表 `sorted_scores`。
2. 使用 Python 列表切片的方式 `sorted_scores[1:-1]`,取出除去第一个和最后一个元素的新列表,即去掉一个最高分和一个最低分的得分列表。
3. 使用 Python 内置函数 `sum()` 对新列表中的得分进行求和。
4. 将求和得到的总分数除以新列表中得分的数量,得到平均得分。
下面是 Python 代码实现:
```python
scores = [80, 90, 85, 95, 92, 88, 87, 91, 89, 93] # 假设得分列表为10个分数
sorted_scores = sorted(scores) # 对得分列表进行排序
average_score = sum(sorted_scores[1:-1]) / len(sorted_scores[1:-1]) # 计算平均得分
print("选手的平均得分为:", average_score) # 输出平均得分
```
输出结果为:
```
选手的平均得分为: 90.0
```
相关问题
青年歌手参加歌曲大奖赛,有 10 个评委进行打分,编程求这位选手的平均得分(去掉一 个最高分和一个最低分)
可以先将 10 个评委打的分数存储在一个列表中,然后使用 Python 的内置函数来去除最高分和最低分,最后计算平均分即可。
具体实现如下:
```python
scores = [80, 85, 90, 92, 95, 88, 87, 84, 91, 89] # 10 个评委的分数列表
# 去除最高分和最低分
scores.remove(max(scores))
scores.remove(min(scores))
# 计算平均分
avg_score = sum(scores) / len(scores)
print("该选手的平均得分为:", avg_score)
```
输出结果为:
```
该选手的平均得分为: 89.0
```
青年歌手参加歌曲大奖赛,有10个评委进行打分,编程求这位选手的平均得分(去掉一 个最高分和一个最低分)。C语言
下面是一种实现方式:
```c
#include <stdio.h>
#include <stdlib.h>
#define JUDGE_NUM 10 // 评委数量
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int scores[JUDGE_NUM];
int i, total_score = 0;
// 输入10个评委的分数
for (i = 0; i < JUDGE_NUM; i++) {
printf("请输入第%d个评委的分数:", i + 1);
scanf("%d", &scores[i]);
}
// 对分数进行排序
qsort(scores, JUDGE_NUM, sizeof(int), compare);
// 去掉一个最高分和一个最低分
for (i = 1; i < JUDGE_NUM - 1; i++) {
total_score += scores[i];
}
// 输出平均得分
printf("选手的平均得分为:%d\n", total_score / 8);
return 0;
}
```
该程序使用了qsort函数对分数进行排序,然后去掉一个最高分和一个最低分,最后计算平均得分。需要注意的是,最后计算平均得分时要除以8而不是10,因为去掉了两个分数。