从总分中减去一个最高分和一个最低分,再除以剩余评委人数并保留两位小数。
OJ: luogu
题目 ID: P5726
难度:入门
标签:模拟列表python
日期: 2026-07-15 18:39
题意
有 n 位评委打分。去掉一个最高分和一个最低分后,输出剩余分数的平均值,保留两位小数。
如果最高分或最低分出现多次,也只去掉其中一个。
思路
读入所有分数后,直接计算:
text
(sum(scores) - max(scores) - min(scores)) / (n - 2)因为只去掉一个最高分和一个最低分,所以只减一次 max(scores) 和一次 min(scores)。
最后用 f"{answer:.2f}" 保留两位小数。
这题是列表统计入门题,Python 正解已经直接对应题意,不创建 brute.py。
Python 知识
/home/rainboy/mycode/hugo-blog/content/program_language/python/input_output_and_strings.md:使用list(map(int, input().split()))读取一行评分。/home/rainboy/mycode/hugo-blog/content/program_language/python/oj_input_output_cheatsheet.md:这是“第一行n,第二行数组”的标准格式。sum(scores)求总分,max(scores)求最高分,min(scores)求最低分。f"{answer:.2f}"保留两位小数输出。
代码
python
n = int(input())
scores = list(map(int, input().split()))
total = sum(scores) - max(scores) - min(scores)
answer = total / (n - 2)
print(f"{answer:.2f}")cpp
/**
* Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
* rbook: -> https://rbook.roj.ac.cn https://rbook2.roj.ac.cn
* rainboy的学习导航网站: https://idx.roj.ac.cn
* create_at: 2026-07-27 00:00
* update_at: 2026-07-27 00:00
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; // 评委人数
cin >> n;
int scores[1005]; // 存储所有评分
int sum = 0;
int max_val = 0; // 最高分
int min_val = 101; // 最低分(分数范围 0~100)
for (int i = 0; i < n; i++) {
cin >> scores[i];
sum += scores[i];
if (scores[i] > max_val) max_val = scores[i];
if (scores[i] < min_val) min_val = scores[i];
}
// 去掉一个最高分和一个最低分后取平均
double avg = (double)(sum - max_val - min_val) / (n - 2);
printf("%.2f\n", avg);
return 0;
}Pythonic 写法
去极值平均:
python
n = int(input())
a = list(map(int, input().split()))
print(f'{(sum(a) - max(a) - min(a)) / (n - 2):.2f}')复杂度
sum、max、min 都会扫描列表,时间复杂度是
总结
这题的关键是“只去掉一个最高分和一个最低分”。直接用列表内置统计函数,能让代码和题意保持一致。