【Java】P5738 【深基7.例4】歌唱比赛

【深基7.例4】歌唱比赛

题目描述

n ( n ≤ 100 ) n(n\le 100) n(n100) 名同学参加歌唱比赛,并接受 m ( m ≤ 20 ) m(m\le 20) m(m20) 名评委的评分,评分范围是 0 0 0 10 10 10 分。这名同学的得分就是这些评委给分中去掉一个最高分,去掉一个最低分,剩下 m − 2 m-2 m2 个评分的平均数。请问得分最高的同学分数是多少?评分保留 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));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值