From a25a7cb8640e6ca742c5ddc7561be06ae9ba5781 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, 29 Oct 2024 10:10:11 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E5=95=8F=E9=A1=8CB148?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/src/Main.java | 30 ++++++++++++++++ yitou/src/Participant.java | 28 +++++++++++++++ yitou/src/RankingSystem.java | 49 +++++++++++++++++++++++++ yitou/test/RankingSystemTest.java | 59 +++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 yitou/src/Main.java create mode 100644 yitou/src/Participant.java create mode 100644 yitou/src/RankingSystem.java create mode 100644 yitou/test/RankingSystemTest.java diff --git a/yitou/src/Main.java b/yitou/src/Main.java new file mode 100644 index 0000000..664be4c --- /dev/null +++ b/yitou/src/Main.java @@ -0,0 +1,30 @@ +package src; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Main { + public static void main(final String[] args) { + final Scanner scanner = new Scanner(System.in); + + // 参加者数の取得 + final int participantNum = scanner.nextInt(); + final List participantsList = new ArrayList<>(); + + // 各参加者の得点をリストに格納 + for (int i = 0; i < participantNum; i++) { + final int score = scanner.nextInt(); + participantsList.add(new Participant(score, i)); + } + + final RankingSystem rankingSystem = new RankingSystem(participantsList); + + // 順位出力 + for (Participant participant : rankingSystem.getParticipants()) { + System.out.println(participant.getRank()); + } + + scanner.close(); + } +} diff --git a/yitou/src/Participant.java b/yitou/src/Participant.java new file mode 100644 index 0000000..2cb15e8 --- /dev/null +++ b/yitou/src/Participant.java @@ -0,0 +1,28 @@ +package src; + +public class Participant { + private int score; + private int rank; + private int index; + + public Participant(final int score, final int index) { + this.score = score; + this.index = index; + } + + public int getScore() { + return score; + } + + public int getRank() { + return rank; + } + + public void setRank(final int rank) { + this.rank = rank; + } + + public int getIndex() { + return index; + } +} diff --git a/yitou/src/RankingSystem.java b/yitou/src/RankingSystem.java new file mode 100644 index 0000000..101c68c --- /dev/null +++ b/yitou/src/RankingSystem.java @@ -0,0 +1,49 @@ +package src; + +import java.util.Comparator; +import java.util.List; + +public class RankingSystem { + private List participants; + + public RankingSystem(final List participants) { + this.participants = participants; + calculateRanks(); + } + + private void calculateRanks() { + // スコアを降順にソート + participants.sort(new Comparator() { + @Override + public int compare(final Participant p1, final Participant p2) { + return p2.getScore() - p1.getScore(); + } + }); + + + int currentRank = 1; + for (int i = 0; i < participants.size(); i++) { + if (i > 0 && participants.get(i).getScore() == participants.get(i - 1).getScore()) { + // 同点の場合は同じ順位 + participants.get(i).setRank(participants.get(i - 1).getRank()); + } else { + // 異なる場合は順位を設定 + participants.get(i).setRank(currentRank); + } + currentRank++; + } + + // 入力順で順位を再配置 + participants.sort(new Comparator() { + @Override + public int compare(final Participant p1, final Participant p2) { + return Integer.compare(p1.getIndex(), p2.getIndex()); + } + }); + + } + + public List getParticipants() { + return participants; + } +} diff --git a/yitou/test/RankingSystemTest.java b/yitou/test/RankingSystemTest.java new file mode 100644 index 0000000..210ac24 --- /dev/null +++ b/yitou/test/RankingSystemTest.java @@ -0,0 +1,59 @@ +package test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import src.Participant; +import src.RankingSystem; + +public class RankingSystemTest { + private List participants; + + @BeforeEach + public void setUp() { + participants = new ArrayList<>(); // 初期化 + } + + @Test + public void 三人の競技者で同点がいない場合() { + participants.add(new Participant(100, 0)); + participants.add(new Participant(90, 1)); + participants.add(new Participant(80, 2)); + + final RankingSystem rankingSystem = new RankingSystem(participants); + + assertEquals(1, rankingSystem.getParticipants().get(0).getRank()); + assertEquals(2, rankingSystem.getParticipants().get(1).getRank()); + assertEquals(3, rankingSystem.getParticipants().get(2).getRank()); + } + + @Test + public void 三人の競技者で同点がいる場合() { + participants.add(new Participant(100, 0)); + participants.add(new Participant(100, 1)); + participants.add(new Participant(90, 2)); + + final RankingSystem rankingSystem = new RankingSystem(participants); + + assertEquals(1, rankingSystem.getParticipants().get(0).getRank()); + assertEquals(1, rankingSystem.getParticipants().get(1).getRank()); + assertEquals(3, rankingSystem.getParticipants().get(2).getRank()); + } + + @Test + public void 四人の競技者で同点の組み合わせが複数ある場合() { + participants.add(new Participant(100, 0)); + participants.add(new Participant(90, 1)); + participants.add(new Participant(100, 2)); + participants.add(new Participant(90, 3)); + + final RankingSystem rankingSystem = new RankingSystem(participants); + + assertEquals(1, rankingSystem.getParticipants().get(0).getRank()); + assertEquals(3, rankingSystem.getParticipants().get(1).getRank()); + assertEquals(1, rankingSystem.getParticipants().get(2).getRank()); + assertEquals(3, rankingSystem.getParticipants().get(3).getRank()); + } +} -- GitLab From 9654b79dcb8a70cd29f079e3470cdb77b62c3712 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, 29 Oct 2024 15:17:36 +0900 Subject: [PATCH 2/4] =?UTF-8?q?B148=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/src/Main.java | 6 +++-- yitou/src/RankingSystem.java | 43 ++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/yitou/src/Main.java b/yitou/src/Main.java index 664be4c..a0e8cb9 100644 --- a/yitou/src/Main.java +++ b/yitou/src/Main.java @@ -17,14 +17,16 @@ public class Main { final int score = scanner.nextInt(); participantsList.add(new Participant(score, i)); } + + scanner.close(); final RankingSystem rankingSystem = new RankingSystem(participantsList); + + rankingSystem.calculateRanks(); // 順位出力 for (Participant participant : rankingSystem.getParticipants()) { System.out.println(participant.getRank()); } - - scanner.close(); } } diff --git a/yitou/src/RankingSystem.java b/yitou/src/RankingSystem.java index 101c68c..18b30ff 100644 --- a/yitou/src/RankingSystem.java +++ b/yitou/src/RankingSystem.java @@ -1,5 +1,6 @@ package src; +import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -8,39 +9,33 @@ public class RankingSystem { public RankingSystem(final List participants) { this.participants = participants; - calculateRanks(); } - private void calculateRanks() { - // スコアを降順にソート - participants.sort(new Comparator() { - @Override - public int compare(final Participant p1, final Participant p2) { - return p2.getScore() - p1.getScore(); - } - }); + public void calculateRanks() { + //スコアを降順にソート + Collections.sort(participants, Comparator.comparing(Participant::getScore).reversed()); + - int currentRank = 1; - for (int i = 0; i < participants.size(); i++) { - if (i > 0 && participants.get(i).getScore() == participants.get(i - 1).getScore()) { - // 同点の場合は同じ順位 - participants.get(i).setRank(participants.get(i - 1).getRank()); + int currentRank = 1; + for (int number = 0; number < participants.size(); number++) { + if (number > 0 && isSameScore(number)) { + // 同点の場合は同じ順位 + participants.get(number).setRank(participants.get(number - 1).getRank()); } else { - // 異なる場合は順位を設定 - participants.get(i).setRank(currentRank); + // 異なる場合は順位を設定 + participants.get(number).setRank(currentRank); } currentRank++; - } - + } // 入力順で順位を再配置 - participants.sort(new Comparator() { - @Override - public int compare(final Participant p1, final Participant p2) { - return Integer.compare(p1.getIndex(), p2.getIndex()); - } - }); + Collections.sort(participants, Comparator.comparing(Participant::getIndex)); + + } + //引数indexの参加者のスコアが前の参加者と同じかどうか判定 + private boolean isSameScore(int index) { + return participants.get(index).getScore() == participants.get(index - 1).getScore(); } public List getParticipants() { -- GitLab From d1875110666f22bc21c9df98adc3987b56206c0e 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, 29 Oct 2024 16:06:33 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/test/RankingSystemTest.java | 49 ++++++++++++++++--------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/yitou/test/RankingSystemTest.java b/yitou/test/RankingSystemTest.java index 210ac24..c76f2b4 100644 --- a/yitou/test/RankingSystemTest.java +++ b/yitou/test/RankingSystemTest.java @@ -1,6 +1,6 @@ package test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.BeforeEach; @@ -10,50 +10,51 @@ import src.RankingSystem; public class RankingSystemTest { private List participants; + private RankingSystem rankingSystem; @BeforeEach public void setUp() { - participants = new ArrayList<>(); // 初期化 + participants = new ArrayList<>(); + rankingSystem = new RankingSystem(participants); } @Test - public void 三人の競技者で同点がいない場合() { + public void スコアが異なる複数の参加者の場合() { participants.add(new Participant(100, 0)); participants.add(new Participant(90, 1)); participants.add(new Participant(80, 2)); - - final RankingSystem rankingSystem = new RankingSystem(participants); - - assertEquals(1, rankingSystem.getParticipants().get(0).getRank()); - assertEquals(2, rankingSystem.getParticipants().get(1).getRank()); - assertEquals(3, rankingSystem.getParticipants().get(2).getRank()); + rankingSystem.calculateRanks(); + + assertEquals(1, participants.get(0).getRank()); + assertEquals(2, participants.get(1).getRank()); + assertEquals(3, participants.get(2).getRank()); } @Test - public void 三人の競技者で同点がいる場合() { + public void 同じスコアの参加者がいる場合() { participants.add(new Participant(100, 0)); participants.add(new Participant(100, 1)); participants.add(new Participant(90, 2)); - - final RankingSystem rankingSystem = new RankingSystem(participants); - - assertEquals(1, rankingSystem.getParticipants().get(0).getRank()); - assertEquals(1, rankingSystem.getParticipants().get(1).getRank()); - assertEquals(3, rankingSystem.getParticipants().get(2).getRank()); + rankingSystem.calculateRanks(); + + assertEquals(1, participants.get(0).getRank()); + assertEquals(1, participants.get(1).getRank()); + assertEquals(3, participants.get(2).getRank()); } @Test - public void 四人の競技者で同点の組み合わせが複数ある場合() { + public void 同じスコアをもつ組み合わせが複数ある場合() { participants.add(new Participant(100, 0)); participants.add(new Participant(90, 1)); participants.add(new Participant(100, 2)); participants.add(new Participant(90, 3)); - - final RankingSystem rankingSystem = new RankingSystem(participants); - - assertEquals(1, rankingSystem.getParticipants().get(0).getRank()); - assertEquals(3, rankingSystem.getParticipants().get(1).getRank()); - assertEquals(1, rankingSystem.getParticipants().get(2).getRank()); - assertEquals(3, rankingSystem.getParticipants().get(3).getRank()); + participants.add(new Participant(80, 4)); + rankingSystem.calculateRanks(); + + assertEquals(1, participants.get(0).getRank()); + assertEquals(3, participants.get(1).getRank()); + assertEquals(1, participants.get(2).getRank()); + assertEquals(3, participants.get(3).getRank()); + assertEquals(5, participants.get(4).getRank()); } } -- GitLab From 0ce79c2bff6682873b99e492aaaf26b85ec75a64 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, 29 Oct 2024 16:10:54 +0900 Subject: [PATCH 4/4] =?UTF-8?q?impot=E3=81=AE*=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yitou/test/RankingSystemTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yitou/test/RankingSystemTest.java b/yitou/test/RankingSystemTest.java index c76f2b4..56ff5d3 100644 --- a/yitou/test/RankingSystemTest.java +++ b/yitou/test/RankingSystemTest.java @@ -1,6 +1,6 @@ package test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.BeforeEach; -- GitLab