【华为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方法即可实现题目要求

题解

java 复制代码
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),查看当前专栏更新的所有题目。

相关推荐
fouryears_2341710 分钟前
深入拆解Spring核心思想之一:IoC
java·后端·spring
codervibe20 分钟前
使用 Spring Boot + JWT 实现多角色登录认证(附完整流程图)
java·后端
坚持学习永不言弃21 分钟前
Ehcache、Caffeine、Memcached和Redis缓存
java
阿劲31 分钟前
从业务卡顿到数据库连接池耗尽:Spring Boot项目HikariCP超时问题实战排查
java·后端·面试
亮11143 分钟前
Maven 编译过程中发生了 Java Heap Space 内存溢出(OutOfMemoryError)
java·开发语言·maven
添乱1 小时前
「Java案例」求PI的值
java
Zhu_S W1 小时前
深入理解Java虚拟机:Java内存区域与内存溢出异常
java·开发语言·jvm
快乐非自愿1 小时前
商品中心—库存分桶高并发的优化文档
java·前端·spring
鸡蛋灌Bean1 小时前
Java常用设计模式大全
java·开发语言·设计模式
喝可乐的布偶猫1 小时前
Java-----韩顺平单例设计模式学习笔记
java·笔记·设计模式