diff --git a/yitou/src/Main.java b/yitou/src/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..a0e8cb9b52142f8cdcbe14a0d02c038bb722d58f --- /dev/null +++ b/yitou/src/Main.java @@ -0,0 +1,32 @@ +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)); + } + + scanner.close(); + + final RankingSystem rankingSystem = new RankingSystem(participantsList); + + rankingSystem.calculateRanks(); + + // 順位出力 + for (Participant participant : rankingSystem.getParticipants()) { + System.out.println(participant.getRank()); + } + } +} diff --git a/yitou/src/Participant.java b/yitou/src/Participant.java new file mode 100644 index 0000000000000000000000000000000000000000..2cb15e81b5355401a2218163dc79c9ad0d827df8 --- /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 0000000000000000000000000000000000000000..18b30ffb2e5f5aab75490ba4c97bc2bb37ea4063 --- /dev/null +++ b/yitou/src/RankingSystem.java @@ -0,0 +1,44 @@ +package src; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class RankingSystem { + private List participants; + + public RankingSystem(final List participants) { + this.participants = participants; + } + + public void calculateRanks() { + //スコアを降順にソート + Collections.sort(participants, Comparator.comparing(Participant::getScore).reversed()); + + + + 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(number).setRank(currentRank); + } + currentRank++; + } + // 入力順で順位を再配置 + 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() { + return participants; + } +} diff --git a/yitou/test/RankingSystemTest.java b/yitou/test/RankingSystemTest.java new file mode 100644 index 0000000000000000000000000000000000000000..56ff5d339597daba521c050340b6cdf31e94e631 --- /dev/null +++ b/yitou/test/RankingSystemTest.java @@ -0,0 +1,60 @@ +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; + private RankingSystem rankingSystem; + + @BeforeEach + public void setUp() { + participants = new ArrayList<>(); + rankingSystem = new RankingSystem(participants); + } + + @Test + public void スコアが異なる複数の参加者の場合() { + participants.add(new Participant(100, 0)); + participants.add(new Participant(90, 1)); + participants.add(new Participant(80, 2)); + rankingSystem.calculateRanks(); + + assertEquals(1, participants.get(0).getRank()); + assertEquals(2, participants.get(1).getRank()); + assertEquals(3, participants.get(2).getRank()); + } + + @Test + public void 同じスコアの参加者がいる場合() { + participants.add(new Participant(100, 0)); + participants.add(new Participant(100, 1)); + participants.add(new Participant(90, 2)); + rankingSystem.calculateRanks(); + + assertEquals(1, participants.get(0).getRank()); + assertEquals(1, participants.get(1).getRank()); + assertEquals(3, participants.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)); + 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()); + } +}