题目描述
一个有 N N N 个选手参加比赛,选手编号为 1 ∽ N ( 3 ≤ N ≤ 100 ) 1 \backsim N(3≤N≤100) 1∽N(3≤N≤100),有M(3≤M≤10)个评委对选手进行打分。
打分规则为每个评委对选手打分,最高分10分,最低分1分。
请计算得分最多的3位选手的编号。
如果得分相同,则得分高分值最多的选手排名靠前(10分数量相同,则比较9分的数量,以此类推,用例中不会出现多个选手得分完全相同的情况)。
输入描述
第一行为半角逗号分割的两个正整数,第一个数字表示 M ( 3 ≤ M ≤ 10 ) M(3≤M≤10) M(3≤M≤10)个评委,第二个数字表示 N ( 3 ≤ N ≤ 100 ) N(3≤N≤100) N(3≤N≤100)个选手。
第2到 M + 1 M+1 M+1 行是半角逗号分割的整数序列,表示评委为每个选手的打分,0号下标数字表示1号选手分数,1号下标数字表示2号选手分数,依次类推。
输出描述
选手前3名的编号。
注:若输入为异常,输出-1,如 M M M、 N N 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,无效分数
题解
将选手编号与分数封装成一个对象。
按照题目要求,编写排序规则
源码
import java.util.*;public class Racing {static Input input;static {input = new Input("4,5\n" +"10,6,9,7,6\n" +"9,10,6,7,5\n" +"8,10,6,5,10\n" +"9,10,8,4,9");input = new Input("2,5\n" +"7,3, 5,4,2\n" +"8,5,4,4,3");input = new Input("4,2\n" +"8,5\n" +"5.6\n" +"10,4\n" +"8,9");input = new Input("4,5\n" +"11,6,9,7,8\n" +"9,10,6,7,8\n" +"8,10,6,9,7\n" +"9,10,8,6,7");}public static void main(String[] args) {try {String[] ss = input.nextLine().split(",");int referees = Integer.parseInt(ss[0]);int players = Integer.parseInt(ss[1]);if (players > 100 || players < 3 || referees > 10 || referees < 3) {System.out.println(-1);return;}// 解析打分输入int[][] socres = new int[referees][players];for (int i = 0; i < referees; i++) {if (!input.hasNextLine()) {System.out.println(-1);return;}String line = input.nextLine();String[] split = line.split(",");if (split.length != players) {System.out.println(-1);return;}for (int j = 0; j < players; j++) {int score = Integer.parseInt(split[j].trim());if (score < 1 || score > 10) {System.out.println(-1);return;}socres[i][j] = score;}}List<Player> playerList = new ArrayList<>();for (int i = 0; i < players; i++) {Integer[] scores = new Integer[referees];for (int j = 0; j < referees; j++) {scores[j] = socres[j][i];}Arrays.sort(scores, (o1, o2) -> o2 - o1);Player player = new Player(i+1, scores);playerList.add(player);}Collections.sort(playerList);StringBuilder append = new StringBuilder().append(playerList.get(0).no).append(",");append.append(playerList.get(1).no).append(",");append.append(playerList.get(2).no);System.out.println(append.toString());} catch (Exception e) {System.out.println(-1);e.printStackTrace();}}static class Player implements Comparable<Player>{public int no;public int score_total;Integer[] scores;public Player(int no, Integer[] scores) {this.no = no;this.scores = scores;for (int i = 0; i < scores.length; i++) {this.score_total += scores[i];}}public int compareTo(Player o) {if (this.score_total == o.score_total) {for (int i = 0; i < scores.length; i++) {if (this.scores[i] != o.scores[i]) {return o.scores[i] - this.scores[i];}}}return o.score_total - this.score_total;}}}