12.华为OD机试 - N个选手比赛前三名、比赛(Java 双机位A卷 100分)

一、题目描述

一个有 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:

1、输入

java 复制代码
4,5
10,6,9,7,6
9,10,6,7,5
8,10,6,5,10
9,10,8,4,9

2、输出

java 复制代码
2,1,5

测试用例 2:

1、输入

java 复制代码
3,3
10,9,8
9,8,7
8,7,6

2、输出

java 复制代码
1 2 3

五、代码实现

java 复制代码
package com.study.algorithm.huaweiOrOD.huaweiOD202509082334.华为OD机试N个选手比赛前三名;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @ProjectName: algorithm
 * @ClassName: Contest
 * @Description: 华为OD机试-N个选手比赛前三名、比赛(Java 2024 E卷 100分)
 * @Author: Tony_Yi
 * @Date: 2025/12/6 21:52
 * @Version 1.0
 **/
public class Contest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] firstLine = sc.nextLine().split(",");
        int M = Integer.parseInt(firstLine[0]);
        int N = Integer.parseInt(firstLine[1]);

        // 边界条件判断
        if (M < 3 || M > 10 || N < 3 || N > 100) {
            System.out.println(-1);
            return;
        }

        // 读取分数
        int[][] scores = new int[M][N];
        for (int i = 0; i < M; i++) {
            String[] parts = sc.nextLine().split(",");
            for (int j = 0; j < N; j++) {
                scores[i][j] = Integer.parseInt(parts[j]);
                if (scores[i][j] < 1 || scores[i][j] > 10) {
                    System.out.println(-1);
                    return;
                }
            }
        }

        // 统计每个参赛选手的得分
        List<Player> players = new ArrayList<>();
        for (int j = 0; j < N; j++) {
            int total = 0;
            int[] count = new int[11];
            for (int i = 0; i < M; i++) {
                total += scores[i][j];
                // 分数是计数数组的索引,数组元素的值表示分数的得分个数
                count[scores[i][j]]++;
            }
            players.add(new Player(j + 1, total, count));
        }

        // 排序
        players.sort((a, b) -> {
            if (b.total != a.total) {
                return b.total - a.total;
            }
            for (int i = 10; i >= 1; i--) {
                if (b.count[i] != a.count[i]) {
                    return b.count[i] - a.count[i];
                }
            }
            return 0;
        });

        for (int i = 0; i < 3; i++) {
            System.out.print(players.get(i).id + " ");
        }
    }

    static class Player {
        int id;
        int total;
        int[] count;

        Player(int id, int total, int[] count) {
            this.id = id;
            this.total = total;
            this.count = count;
        }
    }
}
相关推荐
董董灿是个攻城狮9 小时前
AI视觉连载8:传统 CV 之边缘检测
算法
怒放吧德德10 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆12 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
心之语歌14 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊15 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang16 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
AI软著研究员16 小时前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish16 小时前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
Ray Liang17 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
颜酱17 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法