输入说明:首先输入一个整数n代表评委人数然后输入n个数。请按照题目的计算规则计算出平均分然后输出。
假设评委人数为n,评委给出的分数为a1, a2, ..., an。
根据题目的计算规则,首先需要去掉最高分和最低分,然后计算剩余分数的平均值。
为了实现这个计算过程,可以按照以下步骤进行操作:
- 输入n,表示评委人数。
- 输入n个数,表示评委给出的分数。
- 使用一个变量max_score记录最高分,初始值设为负无穷大。
- 使用一个变量min_score记录最低分,初始值设为正无穷大。
- 使用一个变量sum_score记录剩余分数的和,初始值设为0。
- 遍历n个数,做以下操作:
- 如果当前数大于max_score,更新max_score的值为当前数。
- 如果当前数小于min_score,更新min_score的值为当前数。
- 将当前数累加到sum_score中。
- 计算剩余分数的和sum_score减去最高分max_score和最低分min_score,得到剩余分数的和。
- 将剩余分数的和除以(评委人数-2),得到平均分。
- 输出平均分。
以下是一个示例代码实现:
n = int(input("请输入评委人数:"))
scores = []
for i in range(n):
score = int(input("请输入第{}个评委的分数:".format(i+1)))
scores.append(score)
max_score = float('-inf')
min_score = float('inf')
sum_score = 0
for score in scores:
if score > max_score:
max_score = score
if score < min_score:
min_score = score
sum_score += score
sum_score -= (max_score + min_score)
average_score = sum_score / (n-2)
print("平均分为:", average_score)
注意:此代码假设输入的评委人数n大于等于2,否则会出现除以零的错误。在实际应用中应该对输入进行合法性检查
原文地址: https://www.cveoy.top/t/topic/hHIH 著作权归作者所有。请勿转载和采集!