diff --git a/ykoiso/src/B108_FerrisWheel.java b/ykoiso/src/B108_FerrisWheel.java new file mode 100644 index 0000000000000000000000000000000000000000..da85cc55f4e188fb8772e1a7404736929ec96203 --- /dev/null +++ b/ykoiso/src/B108_FerrisWheel.java @@ -0,0 +1,93 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..c65b0af994ef2a710c534f18adcd63464962898f --- /dev/null +++ b/ykoiso/src/B108_kanransya.java @@ -0,0 +1,87 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..b1907b78165572f62268de8cc366fc289f1b2bfa --- /dev/null +++ b/ykoiso/test/B108_FerrisWheelTest.java @@ -0,0 +1,47 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..86219cc87e621ea56a1d7bc5a7582dfbe957baed --- /dev/null +++ b/ykoiso/test/B108_kanransyaTest.java @@ -0,0 +1,43 @@ +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)); + } +} +