From 1b72c9fdb27d8b9f6c5d3d4ea5cd393235944bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Tue, 8 Jul 2025 14:24:15 +0900 Subject: [PATCH 1/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB=5F095?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ntakanabe/src/KaraokeTournament.java | 171 +++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 ntakanabe/src/KaraokeTournament.java diff --git a/ntakanabe/src/KaraokeTournament.java b/ntakanabe/src/KaraokeTournament.java new file mode 100644 index 0000000..0807323 --- /dev/null +++ b/ntakanabe/src/KaraokeTournament.java @@ -0,0 +1,171 @@ +package paiza.src; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * . B095 カラオケ大会の得点計算をするクラス 課題曲の正しい音程とN人の参加者が歌った音程が与えられ、 各音程の誤差に応じて減点し、N人の中での最高得点を出力する + * + * @author ntakanabe + */ +public class KaraokeTournament { + + // カラオケの初期得点 + private static final int INITIAL_SCORE = 100; + + /** + * . 音程の誤差に応じた減点数を定義するEnum + */ + private enum Penalty { + // 誤差範囲(第1、第2引数の範囲)と減点数(第3引数)を紐付け + PERFECT(0, 5, 0), + GREAT(6, 10, 1), + GOOD(11, 20, 2), + NORMAL(21, 30, 3), + BAD(31, Integer.MAX_VALUE, 5); + + private final int minError; + private final int maxError; + private final int penaltyPoints; + + Penalty(final int minError, final int maxError, final int penaltyPoints) { + this.minError = minError; + this.maxError = maxError; + this.penaltyPoints = penaltyPoints; + } + + /** + * . 指定された誤差に対する減点数を返すメソッド + * + * @param error 音程の誤差 + * @return 減点する点数 + */ + public static int getPenaltyByError(final int error) { + + for (final Penalty p : Penalty.values()) { + if (error >= p.minError && error <= p.maxError) { + return p.penaltyPoints; + } + } + // ここには基本到達しない(念のため) + return 0; + } + } + + /** + * . 入力メソッドから入力値を受け取り、最高得点を計算するメソッドで処理をし、その結果を出力メソッドに渡すmainメソッド + * + * @param args コマンドライン引数 + */ + public static void main(final String[] args) { + + final List inputValues = getInput(); + final int singerCount = inputValues.get(0); + final int songLength = inputValues.get(1); + + final List correctPitches = inputValues.subList(2, 2 + songLength); + final List allSingerPitches = inputValues.subList(2 + songLength, inputValues.size()); + + final int maxScore = calculateMaxScore(singerCount, songLength, correctPitches, + allSingerPitches); + + outputResult(maxScore); + } + + /** + * . 標準入力から入力値(参加人数、曲の長さ、正しい音程、参加者の歌唱音程)を読み込む入力メソッド + * + * @return 入力値を含むInteger型のリスト + */ + private static List getInput() { + + final Scanner sc = new Scanner(System.in); + final List allInputs = new ArrayList<>(); + + final int singerCount = sc.nextInt(); + allInputs.add(singerCount); + + final int songLength = sc.nextInt(); + allInputs.add(songLength); + + for (int i = 0; i < songLength; i++) { + allInputs.add(sc.nextInt()); + } + + for (int i = 0; i < singerCount; i++) { + for (int j = 0; j < songLength; j++) { + allInputs.add(sc.nextInt()); + } + } + sc.close(); + + return allInputs; + } + + /** + * . カラオケ参加者の得点を個別に計算し、最高得点を選出する処理メソッド + * + * @param singerCount 参加人数 + * @param songLength 課題曲の長さ + * @param correctPitches 課題曲の正しい音程のリスト + * @param allSingerPitches 大会に参加したすべての人が歌った音程のリスト + * @return singerCount人の中での最高得点 + */ + private static int calculateMaxScore(final int singerCount, final int songLength, + final List correctPitches, final List allSingerPitches) { + + int maxScore = -1; + + // 各参加者の歌唱音程を抽出 + for (int j = 0; j < singerCount; j++) { + final List sungPitches = new ArrayList<>( + allSingerPitches.subList(j * songLength, (j + 1) * songLength)); + + // プレイヤーごとの得点計算を別のメソッドに委譲 + final int singerScore = calculateSingerScore(songLength, correctPitches, sungPitches); + + if (singerScore > maxScore) { + maxScore = singerScore; + } + } + return maxScore; + } + + /** + * . 一人のプレイヤーのカラオケ得点を計算するメソッド 採点は100点からの減点方式で、音程の誤差に応じて点数が引かれ、0点を下回ることはない + * + * @param songLength 曲の長さ + * @param correctPitches 曲の正しい音程のリスト + * @param sungPitches 現在のプレイヤーが歌った音程のリスト + * @return そのプレイヤーの最終得点 + */ + private static int calculateSingerScore(final int songLength, + final List correctPitches, + final List sungPitches) { + + int singerScore = INITIAL_SCORE; + + for (int i = 0; i < songLength; i++) { + final int sungPitch = sungPitches.get(i); + final int error = Math.abs(correctPitches.get(i) - sungPitch); + + singerScore -= Penalty.getPenaltyByError(error); + + if (singerScore < 0) { + singerScore = 0; + } + } + return singerScore; + } + + /** + * . 計算された最高得点を出力 + * + * @param maxScore 最高得点 + */ + private static void outputResult(final int maxScore) { + + System.out.println(maxScore); + } +} -- GitLab From ee8b8f82f3e192cc41536d7d4f53d746f28fc758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Tue, 8 Jul 2025 16:37:44 +0900 Subject: [PATCH 2/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB=5F095?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E5=86=8D=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ntakanabe/src/KaraokeTournament.java | 109 ++++++++++++++++----------- 1 file changed, 67 insertions(+), 42 deletions(-) diff --git a/ntakanabe/src/KaraokeTournament.java b/ntakanabe/src/KaraokeTournament.java index 0807323..633ae40 100644 --- a/ntakanabe/src/KaraokeTournament.java +++ b/ntakanabe/src/KaraokeTournament.java @@ -5,7 +5,7 @@ import java.util.List; import java.util.Scanner; /** - * . B095 カラオケ大会の得点計算をするクラス 課題曲の正しい音程とN人の参加者が歌った音程が与えられ、 各音程の誤差に応じて減点し、N人の中での最高得点を出力する + * B095 カラオケ大会の得点計算をするクラス. 課題曲の正しい音程とN人の参加者が歌った音程が与えられ、 各音程の誤差に応じて減点し、N人の中での最高得点を出力する. * * @author ntakanabe */ @@ -15,9 +15,10 @@ public class KaraokeTournament { private static final int INITIAL_SCORE = 100; /** - * . 音程の誤差に応じた減点数を定義するEnum + * 音程の誤差に応じた減点数を定義するEnum. */ private enum Penalty { + // 誤差範囲(第1、第2引数の範囲)と減点数(第3引数)を紐付け PERFECT(0, 5, 0), GREAT(6, 10, 1), @@ -36,7 +37,7 @@ public class KaraokeTournament { } /** - * . 指定された誤差に対する減点数を返すメソッド + * 指定された誤差に対する減点数を返すメソッド. * * @param error 音程の誤差 * @return 減点する点数 @@ -54,57 +55,81 @@ public class KaraokeTournament { } /** - * . 入力メソッドから入力値を受け取り、最高得点を計算するメソッドで処理をし、その結果を出力メソッドに渡すmainメソッド - * - * @param args コマンドライン引数 + * 標準入力から入力値(参加人数、曲の長さ、正しい音程、参加者の歌唱音程)を読み込むクラス. */ - public static void main(final String[] args) { + public static class LoadParameter { + private int singerCount; + private int songLength; + private List correctPitches; + private List allSingerPitches; - final List inputValues = getInput(); - final int singerCount = inputValues.get(0); - final int songLength = inputValues.get(1); + /** + * コンストラクタ. + */ + public LoadParameter() { - final List correctPitches = inputValues.subList(2, 2 + songLength); - final List allSingerPitches = inputValues.subList(2 + songLength, inputValues.size()); + final Scanner sc = new Scanner(System.in); - final int maxScore = calculateMaxScore(singerCount, songLength, correctPitches, - allSingerPitches); + this.singerCount = sc.nextInt(); + this.songLength = sc.nextInt(); - outputResult(maxScore); - } + this.correctPitches = new ArrayList<>(); + for (int i = 0; i < this.songLength; i++) { + this.correctPitches.add(sc.nextInt()); + } - /** - * . 標準入力から入力値(参加人数、曲の長さ、正しい音程、参加者の歌唱音程)を読み込む入力メソッド - * - * @return 入力値を含むInteger型のリスト - */ - private static List getInput() { + this.allSingerPitches = new ArrayList<>(); + for (int i = 0; i < this.singerCount; i++) { + for (int j = 0; j < this.songLength; j++) { + this.allSingerPitches.add(sc.nextInt()); + } + } - final Scanner sc = new Scanner(System.in); - final List allInputs = new ArrayList<>(); + sc.close(); + } - final int singerCount = sc.nextInt(); - allInputs.add(singerCount); + public int getSingerCount() { - final int songLength = sc.nextInt(); - allInputs.add(songLength); + return singerCount; + } - for (int i = 0; i < songLength; i++) { - allInputs.add(sc.nextInt()); + public int getSongLength() { + + return songLength; } - for (int i = 0; i < singerCount; i++) { - for (int j = 0; j < songLength; j++) { - allInputs.add(sc.nextInt()); - } + public List getCorrectPitches() { + + return correctPitches; } - sc.close(); - return allInputs; + public List getAllSingerPitches() { + + return allSingerPitches; + } } /** - * . カラオケ参加者の得点を個別に計算し、最高得点を選出する処理メソッド + * 入力メソッドから入力値を受け取り、最高得点を計算するメソッドで処理をし、その結果を出力メソッドに渡すmainメソッド. + * + * @param args コマンドライン引数 + */ + public static void main(final String[] args) { + + final LoadParameter loadParameter = new LoadParameter(); + final int singerCount = loadParameter.getSingerCount(); + final int songLength = loadParameter.getSongLength(); + final List correctPitches = loadParameter.getCorrectPitches(); + final List allSingerPitches = loadParameter.getAllSingerPitches(); + + final int maxScore = calculateMaxScore(singerCount, songLength, correctPitches, + allSingerPitches); + + outputResult(maxScore); + } + + /** + * カラオケ参加者の得点を個別に計算し、最高得点を選出する処理メソッド. * * @param singerCount 参加人数 * @param songLength 課題曲の長さ @@ -118,22 +143,22 @@ public class KaraokeTournament { int maxScore = -1; // 各参加者の歌唱音程を抽出 - for (int j = 0; j < singerCount; j++) { + for (int i = 0; i < singerCount; i++) { final List sungPitches = new ArrayList<>( - allSingerPitches.subList(j * songLength, (j + 1) * songLength)); + allSingerPitches.subList(i * songLength, (i + 1) * songLength)); // プレイヤーごとの得点計算を別のメソッドに委譲 final int singerScore = calculateSingerScore(songLength, correctPitches, sungPitches); if (singerScore > maxScore) { - maxScore = singerScore; + maxScore = Math.max(maxScore, singerScore); } } return maxScore; } /** - * . 一人のプレイヤーのカラオケ得点を計算するメソッド 採点は100点からの減点方式で、音程の誤差に応じて点数が引かれ、0点を下回ることはない + * 一人のプレイヤーのカラオケ得点を計算するメソッド. 採点は100点からの減点方式で、音程の誤差に応じて点数が引かれ、0点を下回ることはない. * * @param songLength 曲の長さ * @param correctPitches 曲の正しい音程のリスト @@ -160,7 +185,7 @@ public class KaraokeTournament { } /** - * . 計算された最高得点を出力 + * 計算された最高得点を出力するメソッド. * * @param maxScore 最高得点 */ -- GitLab From 61f2404e07163de12e672c71ec0346eebb675e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Tue, 8 Jul 2025 17:58:06 +0900 Subject: [PATCH 3/3] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB=5F095?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E5=86=8D=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ntakanabe/src/KaraokeTournament.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ntakanabe/src/KaraokeTournament.java b/ntakanabe/src/KaraokeTournament.java index 633ae40..a55c14b 100644 --- a/ntakanabe/src/KaraokeTournament.java +++ b/ntakanabe/src/KaraokeTournament.java @@ -150,9 +150,7 @@ public class KaraokeTournament { // プレイヤーごとの得点計算を別のメソッドに委譲 final int singerScore = calculateSingerScore(songLength, correctPitches, sungPitches); - if (singerScore > maxScore) { - maxScore = Math.max(maxScore, singerScore); - } + maxScore = Math.max(maxScore, singerScore); } return maxScore; } -- GitLab