From b9eea65058d44bed19174034606638cc27284b11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E5=8B=87=E8=BC=9D?= Date: Tue, 1 Oct 2024 16:46:58 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E5=95=8F=E9=A1=8CB114?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/B114.java | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 yitou/B114.java diff --git a/yitou/B114.java b/yitou/B114.java new file mode 100644 index 0000000..1c5fdf6 --- /dev/null +++ b/yitou/B114.java @@ -0,0 +1,86 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Scanner; + +/** + * 選手の試技結果に基づいて順位付けするプログラムです. + */ +public class B114 { + + public static void main(final String[] args) { + final Scanner scanner = new Scanner(System.in); + + // 試技回数と選手数の入力 + final int tryNum = scanner.nextInt(); + final int playerNum = scanner.nextInt(); + + // 各選手の試技結果 + final List> scores = readScores(scanner, playerNum, tryNum); + + // 選手を降順でソートしてインデックスを取得 + final List rankedPlayers = rankPlayers(scores, playerNum, tryNum); + + // 1位の選手のインデックス出力 + System.out.println(rankedPlayers.get(0) + 1); + + scanner.close(); + } + + /** + * 選手のスコアを昇順にソートしたリストを返す. + */ + private static List> readScores(final Scanner scanner, final int playerNum, final int tryNum) { + final List> scores = new ArrayList<>(); + + for (int i = 0; i < playerNum; i++) { + final List playerScores = new ArrayList<>(); + for (int j = 0; j < tryNum; j++) { + playerScores.add(scanner.nextInt()); + } + Collections.sort(playerScores); + scores.add(playerScores); + } + + return scores; + } + /** + * 降順にソート. + */ + + private static List rankPlayers(final List> scores, final int playerNum, final int tryNum) { + final List idx = new ArrayList<>(); + for (int i = 0; i < playerNum; i++) { + idx.add(i); // 選手のインデックスを保持 + } + + Collections.sort(idx, new SortList(scores)); + + return idx; + } +} + +/** + * リストを降順にソートするためのComparatorクラス. + */ +class SortList implements Comparator { + private final List> scores; + + public SortList(final List> scores) { + this.scores = scores; + } + + /** + * 二人の選手のインデックスを比較. + */ + @Override + public int compare(final Integer a, final Integer b) { + for (int i = scores.get(a).size() - 1; i >= 0; i--) { + if (!scores.get(a).get(i).equals(scores.get(b).get(i))) { + return scores.get(b).get(i) - scores.get(a).get(i); // 降順にする + } + } + return 0; + } +} -- GitLab From 0f15b4099a3b1db6014614620dab2d351747fa0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E5=8B=87=E8=BC=9D?= Date: Tue, 1 Oct 2024 18:03:13 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E5=95=8F=E9=A1=8CB114=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/B114.java | 63 +++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/yitou/B114.java b/yitou/B114.java index 1c5fdf6..9b5c710 100644 --- a/yitou/B114.java +++ b/yitou/B114.java @@ -1,6 +1,5 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.util.Scanner; @@ -20,7 +19,7 @@ public class B114 { final List> scores = readScores(scanner, playerNum, tryNum); // 選手を降順でソートしてインデックスを取得 - final List rankedPlayers = rankPlayers(scores, playerNum, tryNum); + final List rankedPlayers = rankPlayers(scores, playerNum); // 1位の選手のインデックス出力 System.out.println(rankedPlayers.get(0) + 1); @@ -29,8 +28,8 @@ public class B114 { } /** - * 選手のスコアを昇順にソートしたリストを返す. - */ + * 選手のスコアを昇順にソートしたリストを返す. + */ private static List> readScores(final Scanner scanner, final int playerNum, final int tryNum) { final List> scores = new ArrayList<>(); @@ -45,42 +44,34 @@ public class B114 { return scores; } + /** - * 降順にソート. - */ - - private static List rankPlayers(final List> scores, final int playerNum, final int tryNum) { - final List idx = new ArrayList<>(); + * 降順にソート. + * + * @param scores 各プレイヤーのスコアのリスト。リストの各要素はプレイヤーのスコアのリストです. + * @param playerNum プレイヤーの数. + * @return スコアに基づいて降順にソートされたプレイヤーのインデックスのリスト. + */ + private static List rankPlayers(final List> scores, final int playerNum) { + final List idxList = new ArrayList<>(); for (int i = 0; i < playerNum; i++) { - idx.add(i); // 選手のインデックスを保持 + idxList.add(i); // 選手のインデックスを保持 } - Collections.sort(idx, new SortList(scores)); - - return idx; - } -} - -/** - * リストを降順にソートするためのComparatorクラス. - */ -class SortList implements Comparator { - private final List> scores; - - public SortList(final List> scores) { - this.scores = scores; - } + // スコアに基づいて降順でソート + Collections.sort(idxList, (a, b) -> { + List scoresA = scores.get(a); + List scoresB = scores.get(b); - /** - * 二人の選手のインデックスを比較. - */ - @Override - public int compare(final Integer a, final Integer b) { - for (int i = scores.get(a).size() - 1; i >= 0; i--) { - if (!scores.get(a).get(i).equals(scores.get(b).get(i))) { - return scores.get(b).get(i) - scores.get(a).get(i); // 降順にする + for (int i = 0; i < Math.min(scoresA.size(), scoresB.size()); i++) { + int comparison = scoresB.get(i).compareTo(scoresA.get(i)); + if (comparison != 0) { + return comparison; // 降順にする + } } - } - return 0; + // サイズの比較を削除 + return 0; // 同じスコアの場合はインデックス順 + }); + return idxList; } -} +} \ No newline at end of file -- GitLab