【华为OD题库-013】比赛-Java

题目

一共有N个选手参加比赛, 选手编号为1~N (3<=N<=100), 有M (3<=M<=10) 个评委对选手进行打分。
打分规则为:每个评委对选手打分,最高分10分,最低分1分。
请计算得分最多的3位选手的编号。
如果得分相同,则得高分值最多的选手排名靠前, (10分数量相同,则比较9分的数量,以此类推,用例中不会出现多个选手得分完全相同的情况)。
输入描述:
第一行为半角逗号分割的两个正整数,第一个数字表示M (3<=M<=10) 个评委,第二个数字表示N (3<=N<=100) 个选手。
第2到M+1行是半角逗号分割的整数序列,表示评委为每个选手的打分,0号下标数字表示1号选手分数,1号下标数字表示2号选手分数,依次类推。
输出描述:
选手前3名的编号。
注:若输入为异常,输出-1,如M、N、打分不在范围内。
示例1 输入输出 示例仅供调试,后台判题数据一般不包含示例
输入
4, 5
10,6, 9, 7,6
9, 10,6,7,5
8, 10,6, 5, 10
9, 10,8,4, 9
输出
2,1,5
说明
第一行代表有4个评委,5个选手参加吡赛
矩阵代表是4*5,每个数字是选手的编号,每一行代表一个评委对选手的打分排序 。
2号选手得分36分排第1,1号选手36分排第2,5号选手30分(2号10分值有3个,1号10分值只有1个,所以2号排第一)
示例2 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
2, 5
7,3,5,4,2
8,5,4, 4,3
输出
-1
说明
只有2个评委,要求最少为3个评委
示例3 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
4, 2
8, 5
5, 6
10, 4
8, 9
输出
-1
说明
只有2名选手参加,要求最少为3名
示例4 输入输出示例仅供调试,后台判题数据般不包含示例
输入
4,5
11,6,9,7,8
9, 10,6, 7,8
8,10,6,9,7
9, 10,8,6, 7
输出
-1
说明
第一个评委给第一个选手打分11,无效分数

思路

简单的阅读理解题。就是给学生按照成绩降序排序,成绩相同的,按照得高分值数量最多的排序(10分相同,就比较9分,9分数量相同,就比较8分)
涉及对象的比较,可以新建一个Player对象,它有三个属性,学生编号,总分,成绩数组(每个评委打的分数构成的数组)
Player对象中,要实现自定义比较规则,可以实现Comparable接口,重写compareTo方法,先按照总成绩降序排。
如果总成绩相同,那么开始比较得10分的数量,如果10分相同,就比较9分的数量,所以应该还有一个方法,返回在player成绩数组中,得指定分数的个数。
将输入字符串转化为我们的对象加入到集合中,再利用集合的sort方法即可实现题目要求

题解

package hwod;

import java.util.*;
import java.util.stream.Stream;

public class Competition {
    public static void main(String[] args) {
        // 处理输入
        Scanner sc = new Scanner(System.in);
        int[] nums = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        int m = nums[0], n = nums[1];
        if (m < 3 || m > 10 || n < 3 || n > 100) {
            System.out.println(-1);
            return;
        }
        int[][] grids = new int[m][n];
        for (int i = 0; i < m; i++) {
            int[] lines = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
            for (int j = 0; j < lines.length; j++) {
                if (lines[j] < 1 || lines[j] > 10) {
                    System.out.println("-1");
                    return;
                }
                grids[i][j] = lines[j];
            }
        }
        List<Player> playerList = new ArrayList<>();
        for (int j = 0; j < n; j++) {
            int total = 0;
            List<Integer> scores = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                total += grids[i][j];
                scores.add(grids[i][j]);
            }
            playerList.add(new Player(j + 1, total, scores));
        }
        Collections.sort(playerList);

        for (int i = 0; i < 3; i++) {
            System.out.print(playerList.get(i).index);
            if(i!=2) System.out.print(",");
        }

    }
}

class Player implements Comparable<Player> {
    int index;
    int total;
    List<Integer> scores;

    public Player(int index, int total, List<Integer> scores) {
        this.index = index;
        this.total = total;
        this.scores = scores;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<Integer> getScores() {
        return scores;
    }

    public void setScores(List<Integer> scores) {
        this.scores = scores;
    }


    @Override
    public int compareTo(Player other) {
        if (this.total != other.total) return other.total - this.total;
        int maxScore = 10;
        while (maxScore > 0 && singleCount(maxScore, this.scores) == singleCount(maxScore, other.scores)) maxScore--;
        return singleCount(maxScore, other.scores) - singleCount(maxScore, this.scores);
    }

    private int singleCount(int maxScore, List<Integer> scores) {
        int res = 0;
        for (int i = scores.size() - 1; i >= 0; i--) {
            if(maxScore==scores.get(i)) res++;
        }
        return res;
    }
}

推荐

如果你对本系列的其他题目感兴趣,可以参考华为OD机试真题及题解(JAVA),查看当前专栏更新的所有题目。

说明

本专栏所有文章均为原创,欢迎转载,请注明文章出处:https://blog.csdn.net/qq_31076523/article/details/134176793。百度和各类采集站皆不可信,搜索请谨慎鉴别。技术类文章一般都有时效性,本人习惯不定期对自己的博文进行修正和更新,因此请访问出处以查看本文的最新版本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值