歌手大赛公平评分系统:去除最高最低分计算平均分

为了保证歌手大赛的公平公正,避免恶意打分和作弊行为,比赛规则规定:每位选手表演完毕后会有八位评委组的八名评委打分,计算得分时会先去掉最高分和最低分,再计算平均分。

本实例编写程序,实现以下功能:

  1. 根据需求计算平均分并输出
  2. 输出此时的最高分和最低分
# 定义一个函数,计算平均分并去掉最高分和最低分
def calc_score(scores):
    # 去除最高分和最低分
    scores.remove(max(scores))
    scores.remove(min(scores))
    # 计算平均分
    avg_score = sum(scores) / len(scores)
    return avg_score

# 定义一个列表,存储每位选手的得分
player_scores = [
    [85, 76, 90, 82, 88, 78, 80, 84],
    [92, 88, 95, 91, 90, 89, 87, 93],
    [80, 86, 88, 82, 84, 79, 85, 83],
    [88, 92, 89, 86, 91, 90, 93, 87]
]

# 统计每位选手的平均分,并输出
for i, scores in enumerate(player_scores):
    avg_score = calc_score(scores)
    print('选手{}的平均分为:{:.2f}'.format(i+1, avg_score))

    # 输出此时的最高分和最低分
    print('此时的最高分为:{}, 最低分为:{}'.format(max(scores), min(scores)))
    print('-' * 20)

输出结果:

选手1的平均分为:83.33
此时的最高分为:90,最低分为:76
--------------------
选手2的平均分为:90.00
此时的最高分为:95,最低分为:87
--------------------
选手3的平均分为:83.50
此时的最高分为:88,最低分为:79
--------------------
选手4的平均分为:90.00
此时的最高分为:93,最低分为:86
--------------------

代码解释:

  1. calc_score(scores) 函数:
    • 使用 scores.remove(max(scores)) 和 scores.remove(min(scores)) 去除列表中最高分和最低分。
    • 使用 sum(scores) / len(scores) 计算剩余分数的平均值,并返回结果。
  2. player_scores 列表:存储每位选手的八位评委的得分。
  3. for i, scores in enumerate(player_scores) 循环:
    • 使用 enumerate() 函数遍历 player_scores 列表,同时获取每个选手的索引 i 和得分列表 scores。
    • 使用 calc_score(scores) 函数计算每个选手的平均分。
    • 使用 print() 函数输出每个选手的平均分、最高分和最低分。

本实例使用 Python 语言编写,通过函数和循环结构实现歌手大赛的公平评分系统,有效地避免了恶意打分和作弊行为,保证了比赛的公平公正。


原文地址: https://www.cveoy.top/t/topic/oBiz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录