From fbee42ddfa422704c6c5631464874e884e7c92ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=B0=20=E5=95=93=E4=BB=8B?= Date: Thu, 7 Jul 2022 11:22:27 +0900 Subject: [PATCH 1/8] =?UTF-8?q?C038=20=E3=81=8A=E8=8F=93=E5=AD=90=E3=81=AE?= =?UTF-8?q?=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/C038_DivideSnack.java | 77 ++++++++++++++++++++++++++++ knoda/test/C038_DivideSnackTest.java | 43 ++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 knoda/src/C038_DivideSnack.java create mode 100644 knoda/test/C038_DivideSnackTest.java diff --git a/knoda/src/C038_DivideSnack.java b/knoda/src/C038_DivideSnack.java new file mode 100644 index 0000000..2239ea7 --- /dev/null +++ b/knoda/src/C038_DivideSnack.java @@ -0,0 +1,77 @@ +package hellojunit; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; + +public class C038_DivideSnack { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + // 機械の個数(machineNum)とお菓子の個数(snackNum)を入力する + final int machineNum = scan.nextInt(); + final int snackNum = scan.nextInt(); + + // containerMap : 機械の番号と容器の個数を格納するマップ + final Map containerMap = new LinkedHashMap(); + + // 機械ごとの容器の個数を入力 + for (int machineNO = 1; machineNO <= machineNum; machineNO++) { + int containerNum = scan.nextInt(); + containerMap.put(machineNO, containerNum); + } + + // お菓子のあまりが最も少ないかつ、容器が最も多い機械の番号を取得する + DivideSnack divideSnack = new DivideSnack(snackNum, containerMap); + final int machineNO = divideSnack.getMachineNO(); + System.out.println(machineNO); + scan.close(); + } + +} + +// お菓子を分配するクラス +class DivideSnack { + private static int snackNum; + private static Map containerMap; + + public DivideSnack(final int snackNum, final Map containerMap) { + DivideSnack.snackNum = snackNum; + DivideSnack.containerMap = containerMap; + } + + // お菓子のあまりが最も少ないかつ、容器が最も多い機械の番号を取得するメソッド + public int getMachineNO() { + Set machineNOSet = containerMap.keySet(); + + /* + * minRemainerSnack : すべての機械の中でお菓子のあまりが最も少ない数 + * maxContainer : お菓子のあまりが最も少ない機械の中で容器が最も多い数 + * machineNo : minRemainerSnackもしくはmaxContainerを満たす機械の番号 + */ + int minRemainerSnack = snackNum - 1; + int maxContainer = 1; + int machineNO = 1; + + // お菓子のあまりが最も少ないかつ、容器が最も多い機会の番号を探索する + for (Integer mNO : machineNOSet) { + final int remainerSnack = snackNum % containerMap.get(mNO); + + if (minRemainerSnack > remainerSnack) { + // お菓子のあまりが最も少ない機械が単独で存在するとき + minRemainerSnack = remainerSnack; + machineNO = mNO; + + } else if (minRemainerSnack == remainerSnack) { + // お菓子のあまりが最も少ない機械が複数存在するとき + if (maxContainer < containerMap.get(mNO)) { + maxContainer = containerMap.get(mNO); + machineNO = mNO; + } + } + } + return machineNO; + } +} diff --git a/knoda/test/C038_DivideSnackTest.java b/knoda/test/C038_DivideSnackTest.java new file mode 100644 index 0000000..e4e13bd --- /dev/null +++ b/knoda/test/C038_DivideSnackTest.java @@ -0,0 +1,43 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Test; + +public class C038_DivideSnackTest { + + @Test + public void お菓子のあまりが最も少ない機械が複数で存在するとき() { + Map containerMap = new LinkedHashMap(); + int snackNum = 7; + containerMap.put(1, 2); + containerMap.put(2, 3); + containerMap.put(3, 4); + + DivideSnack sut = new DivideSnack(snackNum, containerMap); + int expected = 2; + int actual = sut.getMachineNO(); + assertThat(actual, is(expected)); + } + + @Test + public void お菓子のあまりが最も少ない機械が単独で存在するとき() { + Map containerMap = new LinkedHashMap(); + int snackNum = 15; + containerMap.put(1, 12); + containerMap.put(2, 13); + containerMap.put(3, 7); + containerMap.put(4, 5); + containerMap.put(5, 8); + + DivideSnack sut = new DivideSnack(snackNum, containerMap); + int expected = 4; + int actual = sut.getMachineNO(); + assertThat(actual, is(expected)); + } + +} -- GitLab From c2fb4b4658cb8954943568c8da5018b302c612b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=B0=20=E5=95=93=E4=BB=8B?= Date: Thu, 7 Jul 2022 15:39:50 +0900 Subject: [PATCH 2/8] =?UTF-8?q?C038=20=E3=81=8A=E8=8F=93=E5=AD=90=E3=81=AE?= =?UTF-8?q?=E5=88=86=E9=85=8D=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/C038_DivideSnack.java | 16 ++++++++-------- knoda/test/C038_DivideSnackTest.java | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/knoda/src/C038_DivideSnack.java b/knoda/src/C038_DivideSnack.java index 2239ea7..fa561c7 100644 --- a/knoda/src/C038_DivideSnack.java +++ b/knoda/src/C038_DivideSnack.java @@ -24,8 +24,8 @@ public class C038_DivideSnack { } // お菓子のあまりが最も少ないかつ、容器が最も多い機械の番号を取得する - DivideSnack divideSnack = new DivideSnack(snackNum, containerMap); - final int machineNO = divideSnack.getMachineNO(); + SnackDivider snackDivider = new SnackDivider(snackNum, containerMap); + final int machineNO = snackDivider.getMachineNO(); System.out.println(machineNO); scan.close(); } @@ -33,13 +33,13 @@ public class C038_DivideSnack { } // お菓子を分配するクラス -class DivideSnack { - private static int snackNum; - private static Map containerMap; +class SnackDivider { + private int snackNum; + private Map containerMap; - public DivideSnack(final int snackNum, final Map containerMap) { - DivideSnack.snackNum = snackNum; - DivideSnack.containerMap = containerMap; + public SnackDivider(final int snackNum, final Map containerMap) { + this.snackNum = snackNum; + this.containerMap = containerMap; } // お菓子のあまりが最も少ないかつ、容器が最も多い機械の番号を取得するメソッド diff --git a/knoda/test/C038_DivideSnackTest.java b/knoda/test/C038_DivideSnackTest.java index e4e13bd..4aaabdf 100644 --- a/knoda/test/C038_DivideSnackTest.java +++ b/knoda/test/C038_DivideSnackTest.java @@ -18,7 +18,7 @@ public class C038_DivideSnackTest { containerMap.put(2, 3); containerMap.put(3, 4); - DivideSnack sut = new DivideSnack(snackNum, containerMap); + SnackDivider sut = new SnackDivider(snackNum, containerMap); int expected = 2; int actual = sut.getMachineNO(); assertThat(actual, is(expected)); @@ -34,7 +34,7 @@ public class C038_DivideSnackTest { containerMap.put(4, 5); containerMap.put(5, 8); - DivideSnack sut = new DivideSnack(snackNum, containerMap); + SnackDivider sut = new SnackDivider(snackNum, containerMap); int expected = 4; int actual = sut.getMachineNO(); assertThat(actual, is(expected)); -- GitLab From 894f02f02effb84b7e41cf5aab93504921574c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=B0=20=E5=95=93=E4=BB=8B?= Date: Thu, 21 Jul 2022 11:56:54 +0900 Subject: [PATCH 3/8] =?UTF-8?q?B077=20=E3=83=81=E3=82=B1=E3=83=83=E3=83=88?= =?UTF-8?q?=E3=81=AE=E5=A3=B2=E3=82=8A=E5=A0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B077_TicketOffice.java | 96 +++++++++++++++++++++++++++ knoda/src/C038_DivideSnack.java | 77 --------------------- knoda/test/B077_TicketOfficeTest.java | 58 ++++++++++++++++ knoda/test/C038_DivideSnackTest.java | 43 ------------ 4 files changed, 154 insertions(+), 120 deletions(-) create mode 100644 knoda/src/B077_TicketOffice.java delete mode 100644 knoda/src/C038_DivideSnack.java create mode 100644 knoda/test/B077_TicketOfficeTest.java delete mode 100644 knoda/test/C038_DivideSnackTest.java diff --git a/knoda/src/B077_TicketOffice.java b/knoda/src/B077_TicketOffice.java new file mode 100644 index 0000000..4b459be --- /dev/null +++ b/knoda/src/B077_TicketOffice.java @@ -0,0 +1,96 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B077_TicketOffice { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + // チケットカウンターの数(counterNum)と並んでいる人数(customerNum)を入力する + final int counterNum = scan.nextInt(); + final int customerNum = scan.nextInt(); + + // counterTimeList : それぞれのチケットカウンターで購入するのにかかる時間を格納したリスト + final List counterTimeList = new ArrayList(); + // それぞれのチケットカウンターでの購入時間を入力する + for (int counter = 0; counter < counterNum; counter++) { + counterTimeList.add(scan.nextInt()); + } + + TicketOffice ticketOffice = new TicketOffice(counterTimeList, customerNum); + System.out.println(ticketOffice.getTime()); + scan.close(); + } + +} + +// チケット売り場クラス +class TicketOffice { + private final List counterTimeList; + // currentCounterTimeList : それぞれのチケットカウンターでの、お客様の残り購入時間を格納したリスト + private List currentCounterTimeList = new ArrayList(); + private final int customerNum; + + public TicketOffice(final List counterTimeList, final int customerNum) { + this.counterTimeList = counterTimeList; + this.customerNum = customerNum; + } + + // 経過時間を取得する + public int getTime() { + setUp(); + int time = 0; // time : 経過時間 + int counter; + + for (int customer = 0; customer < customerNum; customer++) { + // カウンターが空いていないとき + while ((counter = getEmptyCounter()) == -1) { + updateCurrentCounterTime(); + time++; + } + currentCounterTimeList.set(counter, counterTimeList.get(counter)); + } + + int maxCounterTime = 0; + for (int c = 0; c < currentCounterTimeList.size(); c++) { + if (maxCounterTime < currentCounterTimeList.get(c)) { + maxCounterTime = currentCounterTimeList.get(c); + } + } + time += maxCounterTime; + return time; + } + + // それぞれのチケットカウンターでの、お客様の残り購入時間を0に初期化 + private void setUp() { + for (int counter = 0; counter < counterTimeList.size(); counter++) { + currentCounterTimeList.add(0); + } + } + + // どのカウンターが空いているかを取得する + private int getEmptyCounter() { + int emptyCounter = -1; + int minCounterTime = 1000; + for (int counter = 0; counter < currentCounterTimeList.size(); counter++) { + if (currentCounterTimeList.get(counter) == 0) { + if (counterTimeList.get(counter) < minCounterTime) { + minCounterTime = counterTimeList.get(counter); + emptyCounter = counter; + } + } + } + return emptyCounter; + } + + // それぞれのカウンターの残り時間をカウントダウンをする + private void updateCurrentCounterTime() { + for (int counter = 0; counter < currentCounterTimeList.size(); counter++) { + currentCounterTimeList.set(counter, currentCounterTimeList.get(counter) - 1); + } + } + +} diff --git a/knoda/src/C038_DivideSnack.java b/knoda/src/C038_DivideSnack.java deleted file mode 100644 index fa561c7..0000000 --- a/knoda/src/C038_DivideSnack.java +++ /dev/null @@ -1,77 +0,0 @@ -package hellojunit; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Scanner; -import java.util.Set; - -public class C038_DivideSnack { - - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - - // 機械の個数(machineNum)とお菓子の個数(snackNum)を入力する - final int machineNum = scan.nextInt(); - final int snackNum = scan.nextInt(); - - // containerMap : 機械の番号と容器の個数を格納するマップ - final Map containerMap = new LinkedHashMap(); - - // 機械ごとの容器の個数を入力 - for (int machineNO = 1; machineNO <= machineNum; machineNO++) { - int containerNum = scan.nextInt(); - containerMap.put(machineNO, containerNum); - } - - // お菓子のあまりが最も少ないかつ、容器が最も多い機械の番号を取得する - SnackDivider snackDivider = new SnackDivider(snackNum, containerMap); - final int machineNO = snackDivider.getMachineNO(); - System.out.println(machineNO); - scan.close(); - } - -} - -// お菓子を分配するクラス -class SnackDivider { - private int snackNum; - private Map containerMap; - - public SnackDivider(final int snackNum, final Map containerMap) { - this.snackNum = snackNum; - this.containerMap = containerMap; - } - - // お菓子のあまりが最も少ないかつ、容器が最も多い機械の番号を取得するメソッド - public int getMachineNO() { - Set machineNOSet = containerMap.keySet(); - - /* - * minRemainerSnack : すべての機械の中でお菓子のあまりが最も少ない数 - * maxContainer : お菓子のあまりが最も少ない機械の中で容器が最も多い数 - * machineNo : minRemainerSnackもしくはmaxContainerを満たす機械の番号 - */ - int minRemainerSnack = snackNum - 1; - int maxContainer = 1; - int machineNO = 1; - - // お菓子のあまりが最も少ないかつ、容器が最も多い機会の番号を探索する - for (Integer mNO : machineNOSet) { - final int remainerSnack = snackNum % containerMap.get(mNO); - - if (minRemainerSnack > remainerSnack) { - // お菓子のあまりが最も少ない機械が単独で存在するとき - minRemainerSnack = remainerSnack; - machineNO = mNO; - - } else if (minRemainerSnack == remainerSnack) { - // お菓子のあまりが最も少ない機械が複数存在するとき - if (maxContainer < containerMap.get(mNO)) { - maxContainer = containerMap.get(mNO); - machineNO = mNO; - } - } - } - return machineNO; - } -} diff --git a/knoda/test/B077_TicketOfficeTest.java b/knoda/test/B077_TicketOfficeTest.java new file mode 100644 index 0000000..fe5f91f --- /dev/null +++ b/knoda/test/B077_TicketOfficeTest.java @@ -0,0 +1,58 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +public class B077_TicketOfficeTest { + List counterTimeList; + int customerNum; + TicketOffice sut; + + @Test + public void チケットカウンターでの購入時間が2_4_5のとき全体で8かかる() { + counterTimeList = new ArrayList(); + int customerNum = 6; + counterTimeList.add(2); + counterTimeList.add(4); + counterTimeList.add(5); + + sut = new TicketOffice(counterTimeList, customerNum); + int expected = 8; + int actual = sut.getTime(); + assertThat(actual, is(expected)); + } + + @Test + public void チケットカウンターでの購入時間が1_1_1_1_100のとき全体で100かかる() { + counterTimeList = new ArrayList(); + int customerNum = 10; + counterTimeList.add(1); + counterTimeList.add(1); + counterTimeList.add(1); + counterTimeList.add(1); + counterTimeList.add(100); + + sut = new TicketOffice(counterTimeList, customerNum); + int expected = 100; + int actual = sut.getTime(); + assertThat(actual, is(expected)); + } + + @Test + public void チケットカウンターでの購入時間が2_1のとき全体で3かかる() { + counterTimeList = new ArrayList(); + int customerNum = 4; + counterTimeList.add(2); + counterTimeList.add(1); + + sut = new TicketOffice(counterTimeList, customerNum); + int expected = 3; + int actual = sut.getTime(); + assertThat(actual, is(expected)); + } +} diff --git a/knoda/test/C038_DivideSnackTest.java b/knoda/test/C038_DivideSnackTest.java deleted file mode 100644 index 4aaabdf..0000000 --- a/knoda/test/C038_DivideSnackTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package hellojunit; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - -import java.util.LinkedHashMap; -import java.util.Map; - -import org.junit.Test; - -public class C038_DivideSnackTest { - - @Test - public void お菓子のあまりが最も少ない機械が複数で存在するとき() { - Map containerMap = new LinkedHashMap(); - int snackNum = 7; - containerMap.put(1, 2); - containerMap.put(2, 3); - containerMap.put(3, 4); - - SnackDivider sut = new SnackDivider(snackNum, containerMap); - int expected = 2; - int actual = sut.getMachineNO(); - assertThat(actual, is(expected)); - } - - @Test - public void お菓子のあまりが最も少ない機械が単独で存在するとき() { - Map containerMap = new LinkedHashMap(); - int snackNum = 15; - containerMap.put(1, 12); - containerMap.put(2, 13); - containerMap.put(3, 7); - containerMap.put(4, 5); - containerMap.put(5, 8); - - SnackDivider sut = new SnackDivider(snackNum, containerMap); - int expected = 4; - int actual = sut.getMachineNO(); - assertThat(actual, is(expected)); - } - -} -- GitLab From 910abf1820c1edadd74b219b16ac7f5d62c57063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=B0=20=E5=95=93=E4=BB=8B?= Date: Fri, 22 Jul 2022 13:30:55 +0900 Subject: [PATCH 4/8] =?UTF-8?q?B067=20=E3=82=BF=E3=82=B9=E3=82=AF=E3=81=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B067_TaskProcess.java | 121 ++++++++++++++++++++++++++ knoda/src/B077_TicketOffice.java | 96 -------------------- knoda/test/B067_TaskProcessTest.java | 98 +++++++++++++++++++++ knoda/test/B077_TicketOfficeTest.java | 58 ------------ 4 files changed, 219 insertions(+), 154 deletions(-) create mode 100644 knoda/src/B067_TaskProcess.java delete mode 100644 knoda/src/B077_TicketOffice.java create mode 100644 knoda/test/B067_TaskProcessTest.java delete mode 100644 knoda/test/B077_TicketOfficeTest.java diff --git a/knoda/src/B067_TaskProcess.java b/knoda/src/B067_TaskProcess.java new file mode 100644 index 0000000..8a3236f --- /dev/null +++ b/knoda/src/B067_TaskProcess.java @@ -0,0 +1,121 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B067_TaskProcess { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + final int taskNum = scan.nextInt(); // taskNum : タスクの数 + final List> taskList = new ArrayList>(); + for (int task = 0; task < taskNum; task++) { + final List scheduleList = new ArrayList(); + scheduleList.add(scan.nextInt()); // 必要日数 + scheduleList.add(scan.nextInt()); // 開始日 + scheduleList.add(scan.nextInt()); // 終了日 + taskList.add(scheduleList); + } + TaskProcess taskProcess = new TaskProcess(taskList); + + if (taskProcess.isCanFinishTask()) { + // すべてのタスクを終了することができたときはyesを出力する + System.out.println("YES"); + } else { + // そうでないときはNoを出力する + System.out.println("NO"); + } + scan.close(); + } + +} + +class TaskProcess { + // taskList : それぞれのタスクの必要日数、開始日、終了日が格納されているリスト + private final List> taskList; + // taskLapsedList : それぞれのタスクの経過時間を格納したリスト + private List taskLapsedList = new ArrayList(); + private final int TIME_DAY_INDEX = 0; // taskListに存在している、必要日数の要素番号 + private final int START_DAY_INDEX = 1; // taskListに存在している、開始日の要素番号 + private final int FINISH_DAY_INDEX = 2; // taskListに存在している、終了日の要素番号 + + public TaskProcess(final List> taskList) { + this.taskList = taskList; + } + + // すべてのタスクを終了することができるか判定する + public boolean isCanFinishTask() { + // wholeTaskStartDay : すべてのタスクの中で最も早い開始日 + int wholeTaskStartDay = taskList.get(0).get(START_DAY_INDEX); + for (int task = 1; task < taskList.size(); task++) { + if (wholeTaskStartDay > taskList.get(task).get(START_DAY_INDEX)) { + wholeTaskStartDay = taskList.get(task).get(START_DAY_INDEX); + } + } + + // wholeTaskStartDay : すべてのタスクの中で最も遅い終了日 + int wholeTaskFinishDay = taskList.get(0).get(FINISH_DAY_INDEX); + for (int task = 1; task < taskList.size(); task++) { + if (wholeTaskFinishDay < taskList.get(task).get(FINISH_DAY_INDEX)) { + wholeTaskFinishDay = taskList.get(task).get(FINISH_DAY_INDEX); + } + } + + setUp(); + // すべてのタスクの開始日から終了日までのタスクの経過状況を調べる + for (int day = wholeTaskStartDay; day <= wholeTaskFinishDay; day++) { + + // primaryTask : 実行するタスクを取得する + final int primaryTask = getPrimaryTask(day); + if (primaryTask == -1) { + // タスクが実行できないときはfalseを返す + return false; + } + // タスクの経過時間を更新する + taskLapsedList.set(primaryTask, taskLapsedList.get(primaryTask) + 1); + + // isFinishTask : タスクが終了したかの判定 + boolean isFinishTask = true; + for (int task = 0; task < taskList.size(); task++) { + if (taskList.get(task).get(TIME_DAY_INDEX) != taskLapsedList.get(task)) { + isFinishTask = false; + break; + } + } + + if (isFinishTask) { + break; + } + } + + // すべてのタスクの終了日を迎えた時点で、すべてのタスクを終了できるか判定する + for (int task = 0; task < taskList.size(); task++) { + if (taskList.get(task).get(TIME_DAY_INDEX) != taskLapsedList.get(task)) { + return false; + } + } + return true; + } + + // どのタスクを実行するかを取得する + private int getPrimaryTask(final int day) { + int primaryTask = -1; + for (int task = 0; task < taskList.size(); task++) { + if ((taskList.get(task).get(START_DAY_INDEX) <= day) && (day <= taskList.get(task).get(FINISH_DAY_INDEX)) + && (taskLapsedList.get(task) < taskList.get(task).get(TIME_DAY_INDEX))) { + primaryTask = task; + break; + } + } + return primaryTask; + } + + // それぞれのタスクの実行時間を0として初期化する + private void setUp() { + for (int task = 0; task < taskList.size(); task++) { + taskLapsedList.add(0); + } + } + +} diff --git a/knoda/src/B077_TicketOffice.java b/knoda/src/B077_TicketOffice.java deleted file mode 100644 index 4b459be..0000000 --- a/knoda/src/B077_TicketOffice.java +++ /dev/null @@ -1,96 +0,0 @@ -package hellojunit; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class B077_TicketOffice { - - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - - // チケットカウンターの数(counterNum)と並んでいる人数(customerNum)を入力する - final int counterNum = scan.nextInt(); - final int customerNum = scan.nextInt(); - - // counterTimeList : それぞれのチケットカウンターで購入するのにかかる時間を格納したリスト - final List counterTimeList = new ArrayList(); - // それぞれのチケットカウンターでの購入時間を入力する - for (int counter = 0; counter < counterNum; counter++) { - counterTimeList.add(scan.nextInt()); - } - - TicketOffice ticketOffice = new TicketOffice(counterTimeList, customerNum); - System.out.println(ticketOffice.getTime()); - scan.close(); - } - -} - -// チケット売り場クラス -class TicketOffice { - private final List counterTimeList; - // currentCounterTimeList : それぞれのチケットカウンターでの、お客様の残り購入時間を格納したリスト - private List currentCounterTimeList = new ArrayList(); - private final int customerNum; - - public TicketOffice(final List counterTimeList, final int customerNum) { - this.counterTimeList = counterTimeList; - this.customerNum = customerNum; - } - - // 経過時間を取得する - public int getTime() { - setUp(); - int time = 0; // time : 経過時間 - int counter; - - for (int customer = 0; customer < customerNum; customer++) { - // カウンターが空いていないとき - while ((counter = getEmptyCounter()) == -1) { - updateCurrentCounterTime(); - time++; - } - currentCounterTimeList.set(counter, counterTimeList.get(counter)); - } - - int maxCounterTime = 0; - for (int c = 0; c < currentCounterTimeList.size(); c++) { - if (maxCounterTime < currentCounterTimeList.get(c)) { - maxCounterTime = currentCounterTimeList.get(c); - } - } - time += maxCounterTime; - return time; - } - - // それぞれのチケットカウンターでの、お客様の残り購入時間を0に初期化 - private void setUp() { - for (int counter = 0; counter < counterTimeList.size(); counter++) { - currentCounterTimeList.add(0); - } - } - - // どのカウンターが空いているかを取得する - private int getEmptyCounter() { - int emptyCounter = -1; - int minCounterTime = 1000; - for (int counter = 0; counter < currentCounterTimeList.size(); counter++) { - if (currentCounterTimeList.get(counter) == 0) { - if (counterTimeList.get(counter) < minCounterTime) { - minCounterTime = counterTimeList.get(counter); - emptyCounter = counter; - } - } - } - return emptyCounter; - } - - // それぞれのカウンターの残り時間をカウントダウンをする - private void updateCurrentCounterTime() { - for (int counter = 0; counter < currentCounterTimeList.size(); counter++) { - currentCounterTimeList.set(counter, currentCounterTimeList.get(counter) - 1); - } - } - -} diff --git a/knoda/test/B067_TaskProcessTest.java b/knoda/test/B067_TaskProcessTest.java new file mode 100644 index 0000000..e978e24 --- /dev/null +++ b/knoda/test/B067_TaskProcessTest.java @@ -0,0 +1,98 @@ +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 B067_TaskProcessTest { + List> taskList; + List scheduleList; + TaskProcess sut; + + @Test + public void YESと出力されるテスト() { + taskList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(4, 8, 11)), + new ArrayList(Arrays.asList(7, 4, 15)), + new ArrayList(Arrays.asList(7, 1, 20)) + )); + + sut = new TaskProcess(taskList); + String expected = "YES"; + String actual; + boolean isCanFinishTask = sut.isCanFinishTask(); + + if (isCanFinishTask) { + actual = "YES"; + } else { + actual = "NO"; + } + assertThat(actual, is(expected)); + } + + @Test + public void NOと出力されるテスト() { + taskList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(8, 1, 15)), + new ArrayList(Arrays.asList(4, 8, 11)) + )); + + sut = new TaskProcess(taskList); + String expected = "NO"; + String actual; + boolean isCanFinishTask = sut.isCanFinishTask(); + + if (isCanFinishTask) { + actual = "YES"; + } else { + actual = "NO"; + } + assertThat(actual, is(expected)); + } + + @Test + public void YESと出力される境界値テスト() { + taskList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(2, 3, 4)), + new ArrayList(Arrays.asList(4, 2, 7)) + )); + + sut = new TaskProcess(taskList); + String expected = "YES"; + String actual; + boolean isCanFinishTask = sut.isCanFinishTask(); + + if (isCanFinishTask) { + actual = "YES"; + } else { + actual = "NO"; + } + assertThat(actual, is(expected)); + } + + @Test + public void 境界値テストでNOと出力されるテスト() { + taskList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(2, 3, 4)), + new ArrayList(Arrays.asList(4, 2, 6)) + )); + + sut = new TaskProcess(taskList); + String expected = "NO"; + String actual; + boolean isCanFinishTask = sut.isCanFinishTask(); + + if (isCanFinishTask) { + actual = "YES"; + } else { + actual = "NO"; + } + assertThat(actual, is(expected)); + } + +} diff --git a/knoda/test/B077_TicketOfficeTest.java b/knoda/test/B077_TicketOfficeTest.java deleted file mode 100644 index fe5f91f..0000000 --- a/knoda/test/B077_TicketOfficeTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package hellojunit; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.Test; - -public class B077_TicketOfficeTest { - List counterTimeList; - int customerNum; - TicketOffice sut; - - @Test - public void チケットカウンターでの購入時間が2_4_5のとき全体で8かかる() { - counterTimeList = new ArrayList(); - int customerNum = 6; - counterTimeList.add(2); - counterTimeList.add(4); - counterTimeList.add(5); - - sut = new TicketOffice(counterTimeList, customerNum); - int expected = 8; - int actual = sut.getTime(); - assertThat(actual, is(expected)); - } - - @Test - public void チケットカウンターでの購入時間が1_1_1_1_100のとき全体で100かかる() { - counterTimeList = new ArrayList(); - int customerNum = 10; - counterTimeList.add(1); - counterTimeList.add(1); - counterTimeList.add(1); - counterTimeList.add(1); - counterTimeList.add(100); - - sut = new TicketOffice(counterTimeList, customerNum); - int expected = 100; - int actual = sut.getTime(); - assertThat(actual, is(expected)); - } - - @Test - public void チケットカウンターでの購入時間が2_1のとき全体で3かかる() { - counterTimeList = new ArrayList(); - int customerNum = 4; - counterTimeList.add(2); - counterTimeList.add(1); - - sut = new TicketOffice(counterTimeList, customerNum); - int expected = 3; - int actual = sut.getTime(); - assertThat(actual, is(expected)); - } -} -- GitLab From 1d6956551b852f6e963a454ff89accb277e710f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=B0=20=E5=95=93=E4=BB=8B?= Date: Fri, 22 Jul 2022 17:48:39 +0900 Subject: [PATCH 5/8] =?UTF-8?q?B067=20=E3=82=BF=E3=82=B9=E3=82=AF=E3=81=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B067_TaskProcess.java | 6 +++++- knoda/test/B067_TaskProcessTest.java | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/knoda/src/B067_TaskProcess.java b/knoda/src/B067_TaskProcess.java index 8a3236f..69d8885 100644 --- a/knoda/src/B067_TaskProcess.java +++ b/knoda/src/B067_TaskProcess.java @@ -69,7 +69,11 @@ class TaskProcess { // primaryTask : 実行するタスクを取得する final int primaryTask = getPrimaryTask(day); if (primaryTask == -1) { - // タスクが実行できないときはfalseを返す + // タスクが実行できないとき + // ある時刻でタスクが1つも行われていないとき + if (taskLapsedList.stream().filter(time -> time == 0).count() > 0) { + continue; + } return false; } // タスクの経過時間を更新する diff --git a/knoda/test/B067_TaskProcessTest.java b/knoda/test/B067_TaskProcessTest.java index e978e24..0caa463 100644 --- a/knoda/test/B067_TaskProcessTest.java +++ b/knoda/test/B067_TaskProcessTest.java @@ -95,4 +95,24 @@ public class B067_TaskProcessTest { assertThat(actual, is(expected)); } + @Test + public void ある時刻のときにどのタスクも実行されないテストでYESが出力される() { + taskList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(2, 1, 3)), + new ArrayList(Arrays.asList(3, 5, 9)) + )); + + sut = new TaskProcess(taskList); + String expected = "YES"; + String actual; + boolean isCanFinishTask = sut.isCanFinishTask(); + + if (isCanFinishTask) { + actual = "YES"; + } else { + actual = "NO"; + } + assertThat(actual, is(expected)); + } + } -- GitLab From fe6609fd1d1d60c660755ed562f7b1f43ac03171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=B0=20=E5=95=93=E4=BB=8B?= Date: Mon, 25 Jul 2022 10:53:16 +0900 Subject: [PATCH 6/8] =?UTF-8?q?B084=20=E3=81=8A=E3=81=99=E3=81=99=E3=82=81?= =?UTF-8?q?=E3=81=AE=E3=81=8A=E5=BA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B067_TaskProcess.java | 125 ------------------------ knoda/src/B084_RecommendationStore.java | 112 +++++++++++++++++++++ knoda/test/B067_TaskProcessTest.java | 118 ---------------------- 3 files changed, 112 insertions(+), 243 deletions(-) delete mode 100644 knoda/src/B067_TaskProcess.java create mode 100644 knoda/src/B084_RecommendationStore.java delete mode 100644 knoda/test/B067_TaskProcessTest.java diff --git a/knoda/src/B067_TaskProcess.java b/knoda/src/B067_TaskProcess.java deleted file mode 100644 index 69d8885..0000000 --- a/knoda/src/B067_TaskProcess.java +++ /dev/null @@ -1,125 +0,0 @@ -package hellojunit; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class B067_TaskProcess { - - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - final int taskNum = scan.nextInt(); // taskNum : タスクの数 - final List> taskList = new ArrayList>(); - for (int task = 0; task < taskNum; task++) { - final List scheduleList = new ArrayList(); - scheduleList.add(scan.nextInt()); // 必要日数 - scheduleList.add(scan.nextInt()); // 開始日 - scheduleList.add(scan.nextInt()); // 終了日 - taskList.add(scheduleList); - } - TaskProcess taskProcess = new TaskProcess(taskList); - - if (taskProcess.isCanFinishTask()) { - // すべてのタスクを終了することができたときはyesを出力する - System.out.println("YES"); - } else { - // そうでないときはNoを出力する - System.out.println("NO"); - } - scan.close(); - } - -} - -class TaskProcess { - // taskList : それぞれのタスクの必要日数、開始日、終了日が格納されているリスト - private final List> taskList; - // taskLapsedList : それぞれのタスクの経過時間を格納したリスト - private List taskLapsedList = new ArrayList(); - private final int TIME_DAY_INDEX = 0; // taskListに存在している、必要日数の要素番号 - private final int START_DAY_INDEX = 1; // taskListに存在している、開始日の要素番号 - private final int FINISH_DAY_INDEX = 2; // taskListに存在している、終了日の要素番号 - - public TaskProcess(final List> taskList) { - this.taskList = taskList; - } - - // すべてのタスクを終了することができるか判定する - public boolean isCanFinishTask() { - // wholeTaskStartDay : すべてのタスクの中で最も早い開始日 - int wholeTaskStartDay = taskList.get(0).get(START_DAY_INDEX); - for (int task = 1; task < taskList.size(); task++) { - if (wholeTaskStartDay > taskList.get(task).get(START_DAY_INDEX)) { - wholeTaskStartDay = taskList.get(task).get(START_DAY_INDEX); - } - } - - // wholeTaskStartDay : すべてのタスクの中で最も遅い終了日 - int wholeTaskFinishDay = taskList.get(0).get(FINISH_DAY_INDEX); - for (int task = 1; task < taskList.size(); task++) { - if (wholeTaskFinishDay < taskList.get(task).get(FINISH_DAY_INDEX)) { - wholeTaskFinishDay = taskList.get(task).get(FINISH_DAY_INDEX); - } - } - - setUp(); - // すべてのタスクの開始日から終了日までのタスクの経過状況を調べる - for (int day = wholeTaskStartDay; day <= wholeTaskFinishDay; day++) { - - // primaryTask : 実行するタスクを取得する - final int primaryTask = getPrimaryTask(day); - if (primaryTask == -1) { - // タスクが実行できないとき - // ある時刻でタスクが1つも行われていないとき - if (taskLapsedList.stream().filter(time -> time == 0).count() > 0) { - continue; - } - return false; - } - // タスクの経過時間を更新する - taskLapsedList.set(primaryTask, taskLapsedList.get(primaryTask) + 1); - - // isFinishTask : タスクが終了したかの判定 - boolean isFinishTask = true; - for (int task = 0; task < taskList.size(); task++) { - if (taskList.get(task).get(TIME_DAY_INDEX) != taskLapsedList.get(task)) { - isFinishTask = false; - break; - } - } - - if (isFinishTask) { - break; - } - } - - // すべてのタスクの終了日を迎えた時点で、すべてのタスクを終了できるか判定する - for (int task = 0; task < taskList.size(); task++) { - if (taskList.get(task).get(TIME_DAY_INDEX) != taskLapsedList.get(task)) { - return false; - } - } - return true; - } - - // どのタスクを実行するかを取得する - private int getPrimaryTask(final int day) { - int primaryTask = -1; - for (int task = 0; task < taskList.size(); task++) { - if ((taskList.get(task).get(START_DAY_INDEX) <= day) && (day <= taskList.get(task).get(FINISH_DAY_INDEX)) - && (taskLapsedList.get(task) < taskList.get(task).get(TIME_DAY_INDEX))) { - primaryTask = task; - break; - } - } - return primaryTask; - } - - // それぞれのタスクの実行時間を0として初期化する - private void setUp() { - for (int task = 0; task < taskList.size(); task++) { - taskLapsedList.add(0); - } - } - -} diff --git a/knoda/src/B084_RecommendationStore.java b/knoda/src/B084_RecommendationStore.java new file mode 100644 index 0000000..fd6437d --- /dev/null +++ b/knoda/src/B084_RecommendationStore.java @@ -0,0 +1,112 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.Set; +import java.util.TreeSet; + +public class B084_RecommendationStore { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + final int storeNum = scan.nextInt(); + final int otherUserNum = scan.nextInt(); + final int criterionNum = scan.nextInt(); + + final List oneselfEvaluation = new ArrayList(); + for (int store = 0; store < storeNum; store++) { + oneselfEvaluation.add(scan.nextInt()); + } + + final List> otherEvaluation = new ArrayList>(); + for (int user = 0; user < otherUserNum; user++) { + final List userEvaluation = new ArrayList(); + for (int store = 0; store < storeNum; store++) { + userEvaluation.add(scan.nextInt()); + } + otherEvaluation.add(userEvaluation); + } + + RecommendationStore recommendationStore = new RecommendationStore(); + final Set storeSet = recommendationStore.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + if (storeSet != null) { + int count = 0; + for (Integer store : storeSet) { + if (count != storeSet.size() - 1) { + System.out.print((store + 1) + " "); + } else { + System.out.println(store + 1); + } + count++; + } + } else { + System.out.println("no"); + } + scan.close(); + } + +} + +class RecommendationStore { + private static final int MAX_EVALUATION = 3; + private static final int ZERO_EVALUATION = 0; + + public Set getStore(final List oneselfEvaluation, final List> otherEvaluation, + final int criterionNum) { + final List oneselfMaxEvaluationStore = getMaxEvaluationStore(oneselfEvaluation); + final List resembleUser = new ArrayList(); + for (int user = 0; user < otherEvaluation.size(); user++) { + int count = 0; + for (Integer store : oneselfMaxEvaluationStore) { + if (otherEvaluation.get(user).get(store) == MAX_EVALUATION) { + count++; + } + } + + if (count >= criterionNum) { + resembleUser.add(user); + } + } + + if (resembleUser.size() != 0) { + final List zeroEvaluationStore = getZeroEvaluationStore(oneselfEvaluation); + final Set recommendStore = new TreeSet(); + for (int user = 0; user < otherEvaluation.size(); user++) { + for (Integer store : zeroEvaluationStore) { + if (otherEvaluation.get(user).get(store) == MAX_EVALUATION) { + recommendStore.add(store); + break; + } + } + } + + if (recommendStore.size() == 0) { + return null; + } + + return recommendStore; + } + return null; + } + + private List getMaxEvaluationStore(final List oneselfEvaluation) { + final List maxEvaluationStore = new ArrayList(); + for (int store = 0; store < oneselfEvaluation.size(); store++) { + if (oneselfEvaluation.get(store) == MAX_EVALUATION) { + maxEvaluationStore.add(store); + } + } + return maxEvaluationStore; + } + + private List getZeroEvaluationStore(final List oneselfEvaluation) { + final List zeroEvaluationStore = new ArrayList(); + for (int store = 0; store < oneselfEvaluation.size(); store++) { + if (oneselfEvaluation.get(store) == ZERO_EVALUATION) { + zeroEvaluationStore.add(store); + } + } + return zeroEvaluationStore; + } +} diff --git a/knoda/test/B067_TaskProcessTest.java b/knoda/test/B067_TaskProcessTest.java deleted file mode 100644 index 0caa463..0000000 --- a/knoda/test/B067_TaskProcessTest.java +++ /dev/null @@ -1,118 +0,0 @@ -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 B067_TaskProcessTest { - List> taskList; - List scheduleList; - TaskProcess sut; - - @Test - public void YESと出力されるテスト() { - taskList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(4, 8, 11)), - new ArrayList(Arrays.asList(7, 4, 15)), - new ArrayList(Arrays.asList(7, 1, 20)) - )); - - sut = new TaskProcess(taskList); - String expected = "YES"; - String actual; - boolean isCanFinishTask = sut.isCanFinishTask(); - - if (isCanFinishTask) { - actual = "YES"; - } else { - actual = "NO"; - } - assertThat(actual, is(expected)); - } - - @Test - public void NOと出力されるテスト() { - taskList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(8, 1, 15)), - new ArrayList(Arrays.asList(4, 8, 11)) - )); - - sut = new TaskProcess(taskList); - String expected = "NO"; - String actual; - boolean isCanFinishTask = sut.isCanFinishTask(); - - if (isCanFinishTask) { - actual = "YES"; - } else { - actual = "NO"; - } - assertThat(actual, is(expected)); - } - - @Test - public void YESと出力される境界値テスト() { - taskList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(2, 3, 4)), - new ArrayList(Arrays.asList(4, 2, 7)) - )); - - sut = new TaskProcess(taskList); - String expected = "YES"; - String actual; - boolean isCanFinishTask = sut.isCanFinishTask(); - - if (isCanFinishTask) { - actual = "YES"; - } else { - actual = "NO"; - } - assertThat(actual, is(expected)); - } - - @Test - public void 境界値テストでNOと出力されるテスト() { - taskList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(2, 3, 4)), - new ArrayList(Arrays.asList(4, 2, 6)) - )); - - sut = new TaskProcess(taskList); - String expected = "NO"; - String actual; - boolean isCanFinishTask = sut.isCanFinishTask(); - - if (isCanFinishTask) { - actual = "YES"; - } else { - actual = "NO"; - } - assertThat(actual, is(expected)); - } - - @Test - public void ある時刻のときにどのタスクも実行されないテストでYESが出力される() { - taskList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(2, 1, 3)), - new ArrayList(Arrays.asList(3, 5, 9)) - )); - - sut = new TaskProcess(taskList); - String expected = "YES"; - String actual; - boolean isCanFinishTask = sut.isCanFinishTask(); - - if (isCanFinishTask) { - actual = "YES"; - } else { - actual = "NO"; - } - assertThat(actual, is(expected)); - } - -} -- GitLab From eae71d69353b4dc1567736a6b7fd5ececb6c6943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=B0=20=E5=95=93=E4=BB=8B?= Date: Mon, 25 Jul 2022 13:15:51 +0900 Subject: [PATCH 7/8] =?UTF-8?q?B084=20=E3=81=8A=E3=81=99=E3=81=99=E3=82=81?= =?UTF-8?q?=E3=81=AE=E3=81=8A=E5=BA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/test/B084_RecommendationStoreTest.java | 83 ++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 knoda/test/B084_RecommendationStoreTest.java diff --git a/knoda/test/B084_RecommendationStoreTest.java b/knoda/test/B084_RecommendationStoreTest.java new file mode 100644 index 0000000..7b20035 --- /dev/null +++ b/knoda/test/B084_RecommendationStoreTest.java @@ -0,0 +1,83 @@ +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 java.util.Set; + +import org.junit.Test; + +public class B084_RecommendationStoreTest { + int criterionNum; + List oneselfEvaluation; + List> otherEvaluation; + RecommendationStore sut; + + @Test + public void 自分が3と評価したお店が基準値を満たしていないときはnoと出力() { + criterionNum = 2; + oneselfEvaluation = new ArrayList(Arrays.asList(1, 3, 0, 0, 2)); + otherEvaluation = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(1, 3, 3, 0, 2)), + new ArrayList(Arrays.asList(2, 3, 1, 0, 2)), + new ArrayList(Arrays.asList(1, 3, 0, 0, 2)) + )); + sut = new RecommendationStore(); + Set actual = sut.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set expected = null; + assertThat(actual, is(expected)); + } + + @Test + public void 自分が3と評価したお店が基準値を満たしていないときお店の番号が出力されるテスト() { + criterionNum = 3; + oneselfEvaluation = new ArrayList(Arrays.asList(0, 3, 3, 3, 0)); + otherEvaluation = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(1, 3, 3, 3, 3)), + new ArrayList(Arrays.asList(2, 3, 1, 0, 2)), + new ArrayList(Arrays.asList(1, 3, 3, 3, 2)) + )); + sut = new RecommendationStore(); + Set actualSet = sut.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + List actualList = new ArrayList(actualSet); + List expectedList = new ArrayList(Arrays.asList(5)); + + for (int i = 0; i < actualList.size(); i++) { + assertThat(actualList.get(i) + 1, is(expectedList.get(i))); + } + } + + @Test + public void 自分が3と評価したお店が基準値を満たしているときnoが出力されるテスト() { + criterionNum = 2; + oneselfEvaluation = new ArrayList(Arrays.asList(1, 3, 0, 3, 2)); + otherEvaluation = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(1, 0, 1, 0, 1)), + new ArrayList(Arrays.asList(3, 0, 2, 0, 1)), + new ArrayList(Arrays.asList(3, 0, 3, 0, 3)) + )); + sut = new RecommendationStore(); + Set actual = sut.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set expected = null; + assertThat(actual, is(expected)); + } + + @Test + public void 好みのユーザが自分の行ったことのないお店に対して3と評価していないときはnoと出力() { + criterionNum = 2; + oneselfEvaluation = new ArrayList(Arrays.asList(1, 3, 3, 0, 0)); + otherEvaluation = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(2, 3, 3, 0, 2)), + new ArrayList(Arrays.asList(2, 3, 1, 0, 2)), + new ArrayList(Arrays.asList(1, 3, 3, 1, 2)) + )); + sut = new RecommendationStore(); + Set actual = sut.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set expected = null; + assertThat(actual, is(expected)); + } + +} -- GitLab From fab1c935482b6e090b0a02bebdf601305b7c3d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=B0=20=E5=95=93=E4=BB=8B?= Date: Mon, 25 Jul 2022 16:54:37 +0900 Subject: [PATCH 8/8] =?UTF-8?q?B084=20=E3=81=8A=E3=81=99=E3=81=99=E3=82=81?= =?UTF-8?q?=E3=81=AE=E3=81=8A=E5=BA=97=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B084_RecommendationStore.java | 74 ++++++++++++++------ knoda/test/B084_RecommendationStoreTest.java | 10 +-- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/knoda/src/B084_RecommendationStore.java b/knoda/src/B084_RecommendationStore.java index fd6437d..ee69a37 100644 --- a/knoda/src/B084_RecommendationStore.java +++ b/knoda/src/B084_RecommendationStore.java @@ -10,15 +10,29 @@ public class B084_RecommendationStore { public static void main(String[] args) { Scanner scan = new Scanner(System.in); + + /* + * storeNum : お店の数 + * otherUserNum : 自分以外のユーザーの数 + * criterionNum : 自分と好みが似ていると言える基準の数 + */ final int storeNum = scan.nextInt(); final int otherUserNum = scan.nextInt(); final int criterionNum = scan.nextInt(); + /* + * それぞれのお店に対する自分の評価を入力する + * oneselfEvaluation : それぞれのお店に対する自分の評価を格納したリスト + */ final List oneselfEvaluation = new ArrayList(); for (int store = 0; store < storeNum; store++) { oneselfEvaluation.add(scan.nextInt()); } + /* + * それぞれのお店に対する自分以外のユーザーの評価を入力する + * otherEvaluation : それぞれのお店に対する自分以外のユーザーの評価を格納したリスト + */ final List> otherEvaluation = new ArrayList>(); for (int user = 0; user < otherUserNum; user++) { final List userEvaluation = new ArrayList(); @@ -29,7 +43,9 @@ public class B084_RecommendationStore { } RecommendationStore recommendationStore = new RecommendationStore(); - final Set storeSet = recommendationStore.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + // おすすめのお店を取得する + final Set storeSet = recommendationStore.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); + // おすすめのお店を出力する if (storeSet != null) { int count = 0; for (Integer store : storeSet) { @@ -48,40 +64,61 @@ public class B084_RecommendationStore { } +// おすすめのお店を算出するクラス class RecommendationStore { - private static final int MAX_EVALUATION = 3; - private static final int ZERO_EVALUATION = 0; + private static final int MAX_EVALUATION = 3; // 評価が3のとき + private static final int ZERO_EVALUATION = 0; // 評価が0のとき - public Set getStore(final List oneselfEvaluation, final List> otherEvaluation, + // おすすめのお店を取得するメソッド + public Set getRecommendStore(final List oneselfEvaluation, final List> otherEvaluation, final int criterionNum) { - final List oneselfMaxEvaluationStore = getMaxEvaluationStore(oneselfEvaluation); + + /* + * oneselfMaxEvaluationStore : 自分が3と評価したお店を格納するリスト + * resembleUser : 自分と好みが似ているユーザを格納したリスト + */ + + // 自分が3と評価したお店を取得する + final List oneselfMaxEvaluationStore = getEvaluationStore(oneselfEvaluation, MAX_EVALUATION); final List resembleUser = new ArrayList(); + + // 自分と好みが似ているユーザを算出する for (int user = 0; user < otherEvaluation.size(); user++) { int count = 0; for (Integer store : oneselfMaxEvaluationStore) { if (otherEvaluation.get(user).get(store) == MAX_EVALUATION) { + // 自分とあるユーザの両方が3と評価したお店の個数を算出する count++; } } if (count >= criterionNum) { + // 自分とあるユーザの両方が3と評価したお店の個数が基準値以上のとき resembleUser.add(user); } } if (resembleUser.size() != 0) { - final List zeroEvaluationStore = getZeroEvaluationStore(oneselfEvaluation); + // 自分と好みが似ているユーザが1人以上存在するとき + + /* + * zeroEvaluationStore : 自分が0と評価したお店を格納したリスト + * recommendStore : おすすめのお店を格納したセット + */ + final List zeroEvaluationStore = getEvaluationStore(oneselfEvaluation, ZERO_EVALUATION); final Set recommendStore = new TreeSet(); - for (int user = 0; user < otherEvaluation.size(); user++) { + + // おすすめのお店を算出する + for (Integer user : resembleUser) { for (Integer store : zeroEvaluationStore) { if (otherEvaluation.get(user).get(store) == MAX_EVALUATION) { recommendStore.add(store); - break; } } } if (recommendStore.size() == 0) { + // おすすめのお店が存在しないとき return null; } @@ -90,23 +127,14 @@ class RecommendationStore { return null; } - private List getMaxEvaluationStore(final List oneselfEvaluation) { - final List maxEvaluationStore = new ArrayList(); - for (int store = 0; store < oneselfEvaluation.size(); store++) { - if (oneselfEvaluation.get(store) == MAX_EVALUATION) { - maxEvaluationStore.add(store); - } - } - return maxEvaluationStore; - } - - private List getZeroEvaluationStore(final List oneselfEvaluation) { - final List zeroEvaluationStore = new ArrayList(); + // 自分が評価したお店を取得する + private List getEvaluationStore(final List oneselfEvaluation, final int evaluation) { + final List evaluationStore = new ArrayList(); for (int store = 0; store < oneselfEvaluation.size(); store++) { - if (oneselfEvaluation.get(store) == ZERO_EVALUATION) { - zeroEvaluationStore.add(store); + if (oneselfEvaluation.get(store) == evaluation) { + evaluationStore.add(store); } } - return zeroEvaluationStore; + return evaluationStore; } } diff --git a/knoda/test/B084_RecommendationStoreTest.java b/knoda/test/B084_RecommendationStoreTest.java index 7b20035..372c0b5 100644 --- a/knoda/test/B084_RecommendationStoreTest.java +++ b/knoda/test/B084_RecommendationStoreTest.java @@ -26,13 +26,13 @@ public class B084_RecommendationStoreTest { new ArrayList(Arrays.asList(1, 3, 0, 0, 2)) )); sut = new RecommendationStore(); - Set actual = sut.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set actual = sut.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); Set expected = null; assertThat(actual, is(expected)); } @Test - public void 自分が3と評価したお店が基準値を満たしていないときお店の番号が出力されるテスト() { + public void 自分が3と評価したお店が基準値を満たしているときお店の番号が出力されるテスト() { criterionNum = 3; oneselfEvaluation = new ArrayList(Arrays.asList(0, 3, 3, 3, 0)); otherEvaluation = new ArrayList>(Arrays.asList( @@ -41,7 +41,7 @@ public class B084_RecommendationStoreTest { new ArrayList(Arrays.asList(1, 3, 3, 3, 2)) )); sut = new RecommendationStore(); - Set actualSet = sut.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set actualSet = sut.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); List actualList = new ArrayList(actualSet); List expectedList = new ArrayList(Arrays.asList(5)); @@ -60,7 +60,7 @@ public class B084_RecommendationStoreTest { new ArrayList(Arrays.asList(3, 0, 3, 0, 3)) )); sut = new RecommendationStore(); - Set actual = sut.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set actual = sut.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); Set expected = null; assertThat(actual, is(expected)); } @@ -75,7 +75,7 @@ public class B084_RecommendationStoreTest { new ArrayList(Arrays.asList(1, 3, 3, 1, 2)) )); sut = new RecommendationStore(); - Set actual = sut.getStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set actual = sut.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); Set expected = null; assertThat(actual, is(expected)); } -- GitLab