diff --git a/kshiraishi/src/B095/B095.java b/kshiraishi/src/B095/B095.java new file mode 100644 index 0000000000000000000000000000000000000000..274701baf8c316a0ffb045e5606a82d6f7d9ccbd --- /dev/null +++ b/kshiraishi/src/B095/B095.java @@ -0,0 +1,71 @@ +package B095; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * カラオケ大会 + * + * 下記2点についてコメントいただけると幸いです。
+ * 1.クラスの分け方
+ * 2.クラス内での変数の使い方
+ * + * @author kshiraishi + * @version 1 + */ + +/** + * データ保持クラス + * @param participantCount 参加人数(歌う人数) + * @param songLength 課題曲の長さ(小節数) + * @param correctPitch 課題曲の各小節の正しい音程のリスト + * @param sungPitch 各歌唱者の歌唱音程のリストのリスト + * @param scoreList 各歌唱者の点数のリスト + */ + +public class B095 { + + public static void main(String[] args) { + B095 karaoke = new B095(); + KaraokeData karaokeData = karaoke.initialize(); + ScoreCalculator calculator = new ScoreCalculator(); + MaxScoreFinder search = new MaxScoreFinder(); + + // 人数分スコアを計算しscoreListに格納 + for (int i = 0; i < karaokeData.getParticipantCount(); i++) { + karaokeData + .getScoreList() + .add(calculator.calcScore(karaokeData.getCorrectPitch(), karaokeData.getSungPitch().get(i))); + } + // maxスコアを探索し表示 + System.out.print(search.serchMaxScore(karaokeData.getScoreList())); + + } + + // 初期化メソッド + public KaraokeData initialize() { + Scanner sc = new Scanner(System.in); + int participantCount = sc.nextInt(); + int songLength = sc.nextInt(); + + List correctPitch = new ArrayList<>(); + for (int s = 0; s < songLength; s++) { + correctPitch.add(sc.nextInt()); + } + + List> sungPitch = new ArrayList<>(); + for (int p = 0; p < participantCount; p++) { + List personSungPitch = new ArrayList<>(); + for (int s = 0; s < songLength; s++) { + personSungPitch.add(sc.nextInt()); + } + sungPitch.add(personSungPitch); + } + + KaraokeData karaokeData = new KaraokeData( + participantCount, songLength, correctPitch, sungPitch); + sc.close(); + return karaokeData; + } + +} diff --git a/kshiraishi/src/B095/KaraokeData.java b/kshiraishi/src/B095/KaraokeData.java new file mode 100644 index 0000000000000000000000000000000000000000..ab0273db9e22270815a9cdc5c4750a865eaeafc9 --- /dev/null +++ b/kshiraishi/src/B095/KaraokeData.java @@ -0,0 +1,49 @@ +package B095; +import java.util.ArrayList; +import java.util.List; + +/** + * データ保持クラス + * @param participantCount 参加人数(歌う人数) + * @param songLength 課題曲の長さ(小節数) + * @param correctPitch 課題曲の各小節の正しい音程のリスト + * @param sungPitch 各歌唱者の歌唱音程のリストのリスト + * @param scoreList 各歌唱者の点数のリスト + */ +public class KaraokeData { + + private final int participantCount; + private final int songLength; + private final List correctPitch; + private final List> sungPitch; + private List scoreList; + + public KaraokeData(int participantCount, int songLength, List correctPitch, + List> sungPitch) { + this.participantCount = participantCount; + this.songLength = songLength; + this.correctPitch = correctPitch; + this.sungPitch = sungPitch; + this.scoreList = new ArrayList<>(); + } + + public int getParticipantCount() { + return participantCount; + } + + public int getSongLength() { + return songLength; + } + + public List getCorrectPitch() { + return correctPitch; + } + + public List> getSungPitch() { + return sungPitch; + } + + public List getScoreList() { + return scoreList; + } +} diff --git a/kshiraishi/src/B095/MaxScoreFinder.java b/kshiraishi/src/B095/MaxScoreFinder.java new file mode 100644 index 0000000000000000000000000000000000000000..8cb57b76981e8ab3cb75aad546c3941e528ecce7 --- /dev/null +++ b/kshiraishi/src/B095/MaxScoreFinder.java @@ -0,0 +1,14 @@ +package B095; + +import java.util.Collections; +import java.util.List; + +/** + * 最大の点数を探索するクラス + * + */ +public class MaxScoreFinder { + public int serchMaxScore(List scoreList) { + return Collections.max(scoreList); + } +} \ No newline at end of file diff --git a/kshiraishi/src/B095/ScoreCalculator.java b/kshiraishi/src/B095/ScoreCalculator.java new file mode 100644 index 0000000000000000000000000000000000000000..053abe55bc6c9a240314e23c032b273c6092346c --- /dev/null +++ b/kshiraishi/src/B095/ScoreCalculator.java @@ -0,0 +1,56 @@ +package B095; + +import java.util.List; + +/** + * スコア計算のクラス
+ * 1人の1曲でのスコアを計算 + * + */ +public class ScoreCalculator { + /** + * 正しい音程のリストとうたった音程のリストから点数を計算 + * @param correctPitch 正しい音程のリスト + * @param personalPitch 歌唱者のうたった音程のリスト + * @return 計算したスコアを返す(0を下回ったら0を返す) + */ + public int calcScore(List correctPitch, List personalPitch) { + int score = 100; + int errorHz = 0; + int penaltyScore = 0; + + for (int i = 0; i < personalPitch.size(); i++) { + errorHz = calculatePitchErrorHz(correctPitch.get(i), personalPitch.get(i)); + penaltyScore = calculatePenalty(errorHz); + score -= penaltyScore; + } + if(score < 0) { + return 0; + } + return score; + } + + // 誤差Hz(絶対値)を返す + public int calculatePitchErrorHz(int correctPitch, int personalPitch) { + int pitchError = 0; + if (correctPitch != personalPitch) { + pitchError = Math.abs(correctPitch - personalPitch); + } + return pitchError; + } + + // 減点メソッド + public int calculatePenalty(int pitchError) { + if (pitchError <= 5) { + return 0; + } else if (pitchError <= 10) { + return 1; + } else if (pitchError <= 20) { + return 2; + } else if (pitchError <= 30) { + return 3; + } else { + return 5; + } + } +} \ No newline at end of file