题目描述
现在有 n ( n ≤ 1000 ) n(n \le 1000) n(n≤1000) 位评委给选手打分,分值从 0 0 0 到 10 10 10。需要去掉一个最高分,去掉一个最低分(如果有多个最高或者最低分,也只需要去掉一个),剩下的评分的平均数就是这位选手的得分。现在输入评委人数和他们的打分,请输出选手的最后得分,精确到 2 2 2 位小数。
输入格式
第一行输入一个正整数 n n n,表示有 n n n 个评委。
第二行输入 n n n 个正整数,第 i i i 个正整数表示第 i i i 个评委打出的分值。
输出格式
输出一行一个两位小数,表示选手的最后得分。
样例 #1
样例输入 #1
5
9 5 6 8 9
样例输出 #1
7.67
提示
数据保证,
3
≤
n
≤
1000
3 \leq n \leq 1000
3≤n≤1000,每个评委打出的分值为为
0
0
0 到
10
10
10(含
0
0
0 与
10
10
10)之间的整数。
解析:利用Arrays.sort()快速排序筛选。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] scores = new int[n];
for (int i = 0; i < n; i++) {
scores[i] = scanner.nextInt();
}
double averageScore = calculateAverageScore(scores);
System.out.printf("%.2f%n", averageScore);
}
// Calculate the average score after removing the highest and lowest scores
private static double calculateAverageScore(int[] scores) {
Arrays.sort(scores); // Sort the scores array in ascending order
double sum = 0;
// Calculate the sum of scores excluding the first (lowest) and last (highest) scores
for (int i = 1; i < scores.length - 1; i++) {
sum += scores[i];
}
// Calculate the average score and return it
return sum / (scores.length - 2);
}
}
掌握常见的题型解法是闯关必备的!!!
--------------------蓝桥杯备赛系列正在不断更新中,欢迎各位大佬指导----------------------------------------------