【深基7.例4】歌唱比赛
题目描述
n ( n ≤ 100 ) n(n\le 100) n(n≤100) 名同学参加歌唱比赛,并接受 m ( m ≤ 20 ) m(m\le 20) m(m≤20) 名评委的评分,评分范围是 0 0 0 到 10 10 10 分。这名同学的得分就是这些评委给分中去掉一个最高分,去掉一个最低分,剩下 m − 2 m-2 m−2 个评分的平均数。请问得分最高的同学分数是多少?评分保留 2 2 2 位小数。
输入格式
第一行两个整数
n
,
m
n,m
n,m。
接下来
n
n
n 行,每行各
m
m
m 个整数,表示得分。
输出格式
输出分数最高的同学的分数,保留两位小数。
样例 #1
样例输入 #1
7 6
4 7 2 6 10 7
0 5 0 10 3 10
2 6 8 4 3 6
6 3 6 7 5 8
5 9 3 3 8 1
5 9 9 3 2 0
5 8 0 4 1 10
样例输出 #1
6.00
代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
/**
* @author: ZZJ
* @date: 2023/03/06
* @desc:
*/
public class Main {
private static final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static final PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) {
try {
int[] ints = Arrays.stream(in.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
double max = 0.00;
for (int i = 0; i < ints[0]; i++) {
int[] points = Arrays.stream(in.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
Arrays.sort(points);
int sum = Arrays.stream(points).sum();
int i1 = sum - points[0] - points[points.length - 1];
max = Math.max(max,(double) i1 / (ints[1]-2));
}
out.printf("%.2f",max);
}catch (Exception ignored){
}finally {
out.flush();
}
}
}
Java8最优解
import java.io.*;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(br);
st.nextToken();
int n = (int) st.nval;
st.nextToken();
int m = (int) st.nval;
int max, min, sum;
double avg = 0;
double s = -1;
DecimalFormat df = new DecimalFormat(".00");//保留2位小数
if (m == 2) {
System.out.print(0);//被除数不等于0,2-2=0
return;
}
for (int i = 1; i <= n; i++) {//注意初始化
sum = 0;
max = -1;
min = 11;
for (int j = 1; j <= m; j++) {
st.nextToken();
int score = (int) st.nval;
sum = sum + score;
// System.out.print(sum+" ");
if (max < score) max = score;
if (min > score) min = score;
}
avg = (double) (sum - min - max) / (m - 2);
if (avg >= s) s = avg;
}
System.out.print(df.format(s));
}
}