diff --git a/knoda/src/B010_Offside.java b/knoda/src/B010_Offside.java new file mode 100644 index 0000000000000000000000000000000000000000..ffd971c2fe63ee2d442f4b96d19948d036911bd7 --- /dev/null +++ b/knoda/src/B010_Offside.java @@ -0,0 +1,152 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B010_Offside { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + final int PLAYER_NUM = 11; + + // どのチーム(teamName)の選手(passNo)がパスを出すかを入力する + final char teamName = scan.next().charAt(0); + final int passNo = scan.nextInt(); + + // Aチームのすべての選手がどの位置にいるかを入力する + // teamAPositionList : Aチームのすべての選手がどの位置にいるかを格納するリスト + final List teamAPositionList = new ArrayList(); + for (int teamANo = 1; teamANo <= PLAYER_NUM; teamANo++) { + teamAPositionList.add(scan.nextInt()); + } + + // Bチームのすべての選手がどの位置にいるかを入力する + // teamBPositionList : Bチームのすべての選手がどの位置にいるかを格納するリスト + final List teamBPositionList = new ArrayList(); + for (int teamBNo = 1; teamBNo <= PLAYER_NUM; teamBNo++) { + teamBPositionList.add(scan.nextInt()); + } + + + Offside offside = new Offside(teamName, passNo); + // オフサイドをしている選手を取得 + final List offsideNoList = offside.getNo(teamAPositionList, teamBPositionList); + + if (offsideNoList.size() == 0) { + // オフサイドをしている選手が1人もいないときはNoneを出力する + System.out.println("None"); + } else { + // オフサイドをしている選手を出力する + for (Integer no : offsideNoList) { + System.out.println(no); + } + } + scan.close(); + } + +} + +// オフサイドの情報のクラス +class Offside { + private final char teamName; + private final int passNo; + private List teamPositionList; + private int centerLine = 55; // センターラインの位置 + + public Offside (final char teamName, final int passNo) { + this.teamName = teamName; + this.passNo = passNo; + } + + // オフサイドをしている選手を取得するメソッド + public List getNo(List teamAPositionList, List teamBPositionList) { + + int passNoPosition; + if (teamName == 'A') { + // Aチームの選手がパスを出したとき + passNoPosition = teamAPositionList.get(passNo - 1); + } else { + // Bチームの選手がパスを出したとき + passNoPosition = teamBPositionList.get(passNo - 1); + } + + // secondBackPlayerPosition : 相手チームのゴールから2番目に近い相手選手の位置 + int secondBackPlayerPosition; + List offsideNoList = new ArrayList(); + if (teamName == 'A') { + // Aチームの選手がパスを出したとき + setTeamPositionList(teamAPositionList); + secondBackPlayerPosition = getSecondBackPlayerPosition(teamBPositionList); + } else { + // Bチームの選手がパスを出したとき + teamAPositionList = getSignReverseList(teamAPositionList); + teamBPositionList = getSignReverseList(teamBPositionList); + passNoPosition = (-1) * passNoPosition; + centerLine = (-1) * centerLine; + + setTeamPositionList(teamBPositionList); + secondBackPlayerPosition = getSecondBackPlayerPosition(teamAPositionList); + } + + // 味方チームの選手がオフサイドをしているかを調べる + for (int no = 1; no <= teamPositionList.size(); no++) { + + // パスを受け取る味方の選手が敵チームのエリアにいない場合 + if (teamPositionList.get(no - 1) < centerLine) { + continue; + } + + // 味方の選手がパスを出した選手よりも敵チームのゴールラインの近くにいない場合 + if (passNoPosition >= teamPositionList.get(no - 1)) { + continue; + } + + // ゴールから2番目近い相手チームの選手よりもゴールに近いときはオフサイド + if (teamPositionList.get(no - 1) > secondBackPlayerPosition) { + offsideNoList.add(no); + } + } + + return offsideNoList; + } + + // 相手チームのゴールから2番目に近い相手チームの選手の位置を取得 + private int getSecondBackPlayerPosition(final List teamPositionList) { + /* + * goalkeeperPosition : 相手チームの選手のゴールキーパーの位置(ゴールから最も近い選手の位置) + * secondBackPlayerPosition : 相手チームのゴールから2番目に近い相手チームの選手の位置 + */ + int goalkeeperPosition = teamPositionList.get(0); + int secondBackPlayerPosition = teamPositionList.get(1); + + // 相手チームのゴールから2番目に近い相手チームの選手を調べる + for (int no = 2; no <= teamPositionList.size(); no++) { + if (goalkeeperPosition < teamPositionList.get(no - 1)) { + secondBackPlayerPosition = goalkeeperPosition; + goalkeeperPosition = teamPositionList.get(no - 1); + continue; + } + + if (secondBackPlayerPosition < teamPositionList.get(no - 1)) { + secondBackPlayerPosition = teamPositionList.get(no - 1); + } + } + return secondBackPlayerPosition; + } + + + // セッター + public void setTeamPositionList(List teamPositionList) { + this.teamPositionList = teamPositionList; + } + + // リストの要素の値の符号を反転させるメソッド + private List getSignReverseList(List teamPositionList) { + for (int no = 1; no <= teamPositionList.size(); no++) { + teamPositionList.set(no - 1, (-1) * teamPositionList.get(no - 1)); + } + return teamPositionList; + } + +} diff --git a/knoda/src/C069_Festival.java b/knoda/src/C069_Festival.java deleted file mode 100644 index b64383cdc058fda88a43357cca4ab58d5715cee2..0000000000000000000000000000000000000000 --- a/knoda/src/C069_Festival.java +++ /dev/null @@ -1,82 +0,0 @@ -package hellojunit; - -import java.util.Scanner; - -public class C069_Festival { - - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - final int year = scan.nextInt(); - final int month = scan.nextInt(); - final int day = scan.nextInt(); - final int festivalMonth = scan.nextInt(); - final int festivalDay = scan.nextInt(); - - Festival festival = new Festival(year, month, day, festivalMonth, festivalDay); - System.out.println(festival.getDayUntilFestival()); - - scan.close(); - } -} - - -class Festival { - private int year; - private int month; - private int day; - private final int festivalMonth; - private final int festivalDay; - private static final int ODD_NUMBER_MONTH_DAY = 13; // 奇数月の日数 - private static final int EVEN_NUMBER_MONTH_DAY = 15; // 偶数月の日数 - - public Festival(final int year, final int month, final int day, final int festivalMonth, final int festivalDay) { - this.year = year; - this.month = month; - this.day = day; - this.festivalMonth = festivalMonth; - this.festivalDay = festivalDay; - } - - // ある月の日数を取得するメソッド - private int getDaysOfMonth(final int month) { - if (month % 2 == 0) { - return EVEN_NUMBER_MONTH_DAY; - } - return ODD_NUMBER_MONTH_DAY; - } - - // 祭りが開催される年であるかを判定するメソッド - private boolean isFestivalYear(final int year) { - return year % 4 == 1; - } - - // 年と月をカウントアップするメソッド - private void countUpMonth() { - month++; - if (month > 13) { - month = 1; - year++; - } - } - - // 祭り開催までの残り日数を取得するメソッド - public int getDayUntilFestival() { - int tempYear = year + 1; - int dayUntilFestival = 0; - while (!isFestivalYear(tempYear)) { - tempYear++; - } - - final int festivalYear = tempYear; - - dayUntilFestival += getDaysOfMonth(month) - day; - countUpMonth(); - - while ((year != festivalYear) || (month != festivalMonth)) { - dayUntilFestival += getDaysOfMonth(month); - countUpMonth(); - } - dayUntilFestival += festivalDay; - return dayUntilFestival; - } -} diff --git a/knoda/test/B010_OffsideTest.java b/knoda/test/B010_OffsideTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a9dc0f1eb9b1fe74fb92f80d5c6ecd94a56bd1e8 --- /dev/null +++ b/knoda/test/B010_OffsideTest.java @@ -0,0 +1,78 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +public class B010_OffsideTest { + List teamAPositionList; + List teamBPositionList; + List expectedList; + List actualList; + int passNo; + char teamName; + + @Test + public void Aチームの選手がパスをしたときの取得結果() { + teamAPositionList = new ArrayList(Arrays.asList( + 18, 41, 63, 30, 84, 95, 67, 29, 71, 48, 91 + )); + + teamBPositionList = new ArrayList(Arrays.asList( + 96, 77, 40, 67, 49, 75, 76, 31, 19, 60, 47 + )); + + teamName = 'A'; + passNo = 3; + Offside sut = new Offside(teamName, passNo); + actualList = sut.getNo(teamAPositionList, teamBPositionList); + expectedList = new ArrayList(Arrays.asList(5, 6, 11)); + for (int player = 1; player <= expectedList.size(); player++) { + assertThat(actualList.get(player - 1), is(expectedList.get(player - 1))); + } + } + + @Test + public void Bチームの選手がパスをしたときの取得結果() { + teamAPositionList = new ArrayList(Arrays.asList( + 86, 36, 55, 88, 10, 82, 53, 107, 83, 22, 15 + )); + + teamBPositionList = new ArrayList(Arrays.asList( + 69, 38, 48, 73, 21, 50, 27, 1, 41, 24, 103 + )); + + teamName = 'B'; + passNo = 7; + Offside sut = new Offside(teamName, passNo); + actualList = sut.getNo(teamAPositionList, teamBPositionList); + expectedList = new ArrayList(Arrays.asList(8)); + for (int player = 1; player <= expectedList.size(); player++) { + assertThat(actualList.get(player - 1), is(expectedList.get(player - 1))); + } + } + + @Test + public void オフサイドをしている選手が1人もいないときはリストの要素数が0() { + teamAPositionList = new ArrayList(Arrays.asList( + 69, 41, 96, 89, 53, 42, 83, 95, 48, 4, 25 + )); + + teamBPositionList = new ArrayList(Arrays.asList( + 100, 71, 90, 59, 86, 97, 84, 85, 79, 81, 98 + )); + + teamName = 'B'; + passNo = 10; + Offside sut = new Offside(teamName, passNo); + actualList = sut.getNo(teamAPositionList, teamBPositionList); + expectedList = new ArrayList(Arrays.asList()); + assertThat(actualList.size(), is(expectedList.size())); + } + +} diff --git a/knoda/test/C069_FestivalTest.java b/knoda/test/C069_FestivalTest.java deleted file mode 100644 index cf883eb8f76fc71e41dfabdcc441df2eea29a2a3..0000000000000000000000000000000000000000 --- a/knoda/test/C069_FestivalTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package hellojunit; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - -import org.junit.Test; - -public class C069_FestivalTest { - - @Test - public void 日数を取得するテスト1() { - Festival sut = new Festival(2000, 12, 10, 1, 10); - int expected = 28; - int actual = sut.getDayUntilFestival(); - assertThat(actual, is(expected)); - } - - @Test - public void 日数を取得するテスト2() { - Festival sut = new Festival(1994, 4, 8, 7, 13); - int expected = 591; - int actual = sut.getDayUntilFestival(); - assertThat(actual, is(expected)); - } - -} diff --git a/ykoiso/src/B108_FerrisWheel.java b/ykoiso/src/B108_FerrisWheel.java deleted file mode 100644 index da85cc55f4e188fb8772e1a7404736929ec96203..0000000000000000000000000000000000000000 --- a/ykoiso/src/B108_FerrisWheel.java +++ /dev/null @@ -1,93 +0,0 @@ -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class B108_FerrisWheel { - public static void main(String[] args) { - final Scanner scan = new Scanner(System.in); - // 定数の読み込み - int GONDOLANUM = scan.nextInt(); - int GROUPNUM = scan.nextInt(); - List capacity = new ArrayList<>(); - List group = new ArrayList<>(); - // ゴンドラの乗車上限 - for (int i = 0; i < GONDOLANUM; i++) { - int num = scan.nextInt(); - capacity.add(num); - } - // グループごとの人数 - for (int i = 0; i < GROUPNUM; i++) { - int num = scan.nextInt(); - group.add(num); - } - // 観覧車作成 - B108_FerrisWheel b = new B108_FerrisWheel(); - FerrisWheel ferrisWheel = b.new FerrisWheel(GONDOLANUM, GROUPNUM, capacity, group); - // 乗車人数計算 - ferrisWheel.calcPassengers(); - // 乗車人数の表示 - ferrisWheel.showCountPassengers(); - scan.close(); - } - - class FerrisWheel { - private final int GONDOLANUM, GROUPNUM; - private List gondolaCapacity, countPassengers, groupPeopleNum; - - // コンストラクタ - FerrisWheel(final int GONDOLANUM, final int GROUPNUM, List gondolaCapacity, - List groupPeopleNum) { - this.GONDOLANUM = GONDOLANUM; - this.GROUPNUM = GROUPNUM; - this.gondolaCapacity = gondolaCapacity; - countPassengers = new ArrayList<>(); - for (int i = 0; i < GONDOLANUM; i++) { - countPassengers.add(0); - } - this.groupPeopleNum = groupPeopleNum; - } - - // テスト用 - public List getCountPassengers() { - return countPassengers; - } - - // 乗車人数の計算 - public void calcPassengers() { - // ゴンドラの番号 - int j = 0; - // グループごとの処理 - for (int i = 0; i < GROUPNUM; i++) { - // グループが乗り終わったかの判定 - boolean isRideAll = false; - while (isRideAll == false) { - // グループの人数が乗車上限より多かったら - // 上限人数分乗せてグループの人数を減らす - if (groupPeopleNum.get(i) > gondolaCapacity.get(j)) { - countPassengers.set(j, countPassengers.get(j) + gondolaCapacity.get(j)); - groupPeopleNum.set(i, groupPeopleNum.get(i) - gondolaCapacity.get(j)); - } else { - // グループの人数が乗車上限以下なら - // 上限人数分乗せてフラグを立てる - countPassengers.set(j, countPassengers.get(j) + groupPeopleNum.get(i)); - isRideAll = true; - } - // 次のゴンドラへ - ++j; - // ゴンドラが1周したら最初のゴンドラへ - if (j == GONDOLANUM) { - j = 0; - } - // フラグが立ったら次のグループへ - } - } - } - - // 乗車人数表示 - public void showCountPassengers() { - for (int i = 0; i < GONDOLANUM; i++) { - System.out.println(countPassengers.get(i)); - } - } - } -} diff --git a/ykoiso/src/B108_kanransya.java b/ykoiso/src/B108_kanransya.java deleted file mode 100644 index c65b0af994ef2a710c534f18adcd63464962898f..0000000000000000000000000000000000000000 --- a/ykoiso/src/B108_kanransya.java +++ /dev/null @@ -1,87 +0,0 @@ -import java.util.Scanner; - -public class B108_kanransya { - public static void main(String[] args) { - final Scanner scan = new Scanner(System.in); - // 定数の読み込み - int N = scan.nextInt(); - int M = scan.nextInt(); - int[] capacity = new int[N]; - int[] group = new int[M]; - // ゴンドラの乗車上限 - for (int i = 0; i < N; i++) { - capacity[i] = scan.nextInt(); - } - // グループごとの人数 - for (int i = 0; i < M; i++) { - group[i] = scan.nextInt(); - } - // 観覧車作成 - Kanransya kanransya = new Kanransya(N, M, capacity, group); - // 乗車人数計算 - kanransya.calcPassengers(); - // 乗車人数の表示 - kanransya.showCountPassengers(); - scan.close(); - } - - static class Kanransya { - private int N, M; - private int[] gondoraCapacity, countPassengers, groupNum; - - // コンストラクタ - Kanransya(int N, int M, int[] gondoraCapacity, int[] groupNum) { - this.N = N; - this.M = M; - this.gondoraCapacity = gondoraCapacity; - countPassengers = new int[N]; - this.groupNum = groupNum; - } - - // テスト用 - public int[] getCountPassengers() { - return countPassengers; - } - - // 乗車人数の計算 - public void calcPassengers() { - // ゴンドラの番号 - int j = 0; - // グループごとの処理 - for (int i = 0; i < M; i++) { - // グループが乗り終わったかの判定 - boolean isRideAll = false; - while (true) { - // グループの人数が乗車上限より多かったら - // 上限人数分乗せてグループの人数を減らす - if (groupNum[i] > gondoraCapacity[j]) { - countPassengers[j] += gondoraCapacity[j]; - groupNum[i] -= gondoraCapacity[j]; - } else { - // グループの人数が乗車上限以下なら - // 上限人数分乗せてフラグを立てる - countPassengers[j] += groupNum[i]; - isRideAll = true; - } - // 次のゴンドラへ - ++j; - // ゴンドラが1周したら最初のゴンドラへ - if (j == N) { - j = 0; - } - // フラグが立ったら次のグループへ - if (isRideAll == true) { - break; - } - } - } - } - - // 乗車人数表示 - public void showCountPassengers() { - for (int i = 0; i < N; i++) { - System.out.println(countPassengers[i]); - } - } - } -} diff --git a/ykoiso/test/B108_FerrisWheelTest.java b/ykoiso/test/B108_FerrisWheelTest.java deleted file mode 100644 index b1907b78165572f62268de8cc366fc289f1b2bfa..0000000000000000000000000000000000000000 --- a/ykoiso/test/B108_FerrisWheelTest.java +++ /dev/null @@ -1,47 +0,0 @@ -import static org.junit.Assert.assertThat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.Test; -import static org.hamcrest.CoreMatchers.*; - -public class B108_FerrisWheelTest { - - // ゴンドラは3つ - B108_FerrisWheel b = new B108_FerrisWheel(); - B108_FerrisWheel.FerrisWheel sut; - - @Test - public void 全員乗れた時() { - List gondolaCapacity = new ArrayList<>(Arrays.asList(3, 3, 3)); - List groupPeopleNum = new ArrayList<>(Arrays.asList(3)); - sut = b.new FerrisWheel(3, 1, gondolaCapacity, groupPeopleNum); - sut.calcPassengers(); - List actual = sut.getCountPassengers(); - List expected = new ArrayList<>(Arrays.asList(3, 0, 0)); - assertThat(actual, is(expected)); - } - - @Test - public void 全員乗れなかった時() { - List gondolaCapacity = new ArrayList<>(Arrays.asList(3, 3, 3)); - List groupPeopleNum = new ArrayList<>(Arrays.asList(4)); - sut = b.new FerrisWheel(3, 1, gondolaCapacity, groupPeopleNum); - sut.calcPassengers(); - List actual = sut.getCountPassengers(); - List expected = new ArrayList<>(Arrays.asList(3, 1, 0)); - assertThat(actual, is(expected)); - } - - @Test - public void グループが2つの時() { - List gondolaCapacity = new ArrayList<>(Arrays.asList(3, 3, 3)); - List groupPeopleNum = new ArrayList<>(Arrays.asList(4, 3)); - sut = b.new FerrisWheel(3, 2, gondolaCapacity, groupPeopleNum); - sut.calcPassengers(); - List actual = sut.getCountPassengers(); - List expected = new ArrayList<>(Arrays.asList(3, 1, 3)); - assertThat(actual, is(expected)); - } -} - diff --git a/ykoiso/test/B108_kanransyaTest.java b/ykoiso/test/B108_kanransyaTest.java deleted file mode 100644 index 86219cc87e621ea56a1d7bc5a7582dfbe957baed..0000000000000000000000000000000000000000 --- a/ykoiso/test/B108_kanransyaTest.java +++ /dev/null @@ -1,43 +0,0 @@ -import static org.junit.Assert.assertThat; -import org.junit.Test; -import static org.hamcrest.CoreMatchers.*; - -public class B108_kanransyaTest { - - // ゴンドラは3つ - B108_kanransya.Kanransya sut; - - @Test - public void 全員乗れた時() { - int[] gondoraCapacity = {3, 3, 3}; - int[] groupNum = {3}; - sut = new B108_kanransya.Kanransya(3, 1, gondoraCapacity, groupNum); - sut.calcPassengers(); - int[] actual = sut.getCountPassengers(); - int[] expected = {3, 0, 0}; - assertThat(actual, is(expected)); - } - - @Test - public void 全員乗れなかった時() { - int[] gondoraCapacity = {3, 3, 3}; - int[] groupNum = {4}; - sut = new B108_kanransya.Kanransya(3, 1, gondoraCapacity, groupNum); - sut.calcPassengers(); - int[] actual = sut.getCountPassengers(); - int[] expected = {3, 1, 0}; - assertThat(actual, is(expected)); - } - - @Test - public void グループが2つの時() { - int[] gondoraCapacity = {3, 3, 3}; - int[] groupNum = {4, 3}; - sut = new B108_kanransya.Kanransya(3, 2, gondoraCapacity, groupNum); - sut.calcPassengers(); - int[] actual = sut.getCountPassengers(); - int[] expected = {3, 1, 3}; - assertThat(actual, is(expected)); - } -} -