From 1fe23e95a906b6ba92eb627c1b39916d925ff315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Wed, 6 Jul 2022 15:00:12 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Bao8=20=E8=A6=B3=E8=A6=A7=E8=BB=8A=E3=81=AE?= =?UTF-8?q?=E7=A8=BC=E5=83=8D=E7=8A=B6=E6=B3=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B108_kanransya.java | 87 +++++++++++++++++++++++++++++ ykoiso/test/B108_kanransyaTest.java | 43 ++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 ykoiso/src/B108_kanransya.java create mode 100644 ykoiso/test/B108_kanransyaTest.java diff --git a/ykoiso/src/B108_kanransya.java b/ykoiso/src/B108_kanransya.java new file mode 100644 index 0000000..c65b0af --- /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_kanransyaTest.java b/ykoiso/test/B108_kanransyaTest.java new file mode 100644 index 0000000..86219cc --- /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)); + } +} + -- GitLab From b79603538ee025bff8dedde988f748093f52c853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Thu, 7 Jul 2022 09:25:22 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Bao8=20=E8=A6=B3=E8=A6=A7=E8=BB=8A=E3=81=AE?= =?UTF-8?q?=E7=A8=BC=E5=83=8D=E7=8A=B6=E6=B3=81=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B108_FerrisWheel.java | 93 +++++++++++++++++++++++++++ ykoiso/test/B108_FerrisWheelTest.java | 47 ++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 ykoiso/src/B108_FerrisWheel.java create mode 100644 ykoiso/test/B108_FerrisWheelTest.java diff --git a/ykoiso/src/B108_FerrisWheel.java b/ykoiso/src/B108_FerrisWheel.java new file mode 100644 index 0000000..da85cc5 --- /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/test/B108_FerrisWheelTest.java b/ykoiso/test/B108_FerrisWheelTest.java new file mode 100644 index 0000000..b1907b7 --- /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)); + } +} + -- GitLab