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 01/18] =?UTF-8?q?C038=20=E3=81=8A=E8=8F=93=E5=AD=90?= =?UTF-8?q?=E3=81=AE=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 02/18] =?UTF-8?q?C038=20=E3=81=8A=E8=8F=93=E5=AD=90?= =?UTF-8?q?=E3=81=AE=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 03/18] =?UTF-8?q?B077=20=E3=83=81=E3=82=B1=E3=83=83?= =?UTF-8?q?=E3=83=88=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 04/18] =?UTF-8?q?B067=20=E3=82=BF=E3=82=B9=E3=82=AF?= =?UTF-8?q?=E3=81=AE=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 05/18] =?UTF-8?q?B067=20=E3=82=BF=E3=82=B9=E3=82=AF?= =?UTF-8?q?=E3=81=AE=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 06/18] =?UTF-8?q?B084=20=E3=81=8A=E3=81=99=E3=81=99?= =?UTF-8?q?=E3=82=81=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 07/18] =?UTF-8?q?B084=20=E3=81=8A=E3=81=99=E3=81=99?= =?UTF-8?q?=E3=82=81=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 08/18] =?UTF-8?q?B084=20=E3=81=8A=E3=81=99=E3=81=99?= =?UTF-8?q?=E3=82=81=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 From 9b08ba2e0168aba76121f7666cf4470bb1c14a24 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: Tue, 26 Jul 2022 11:38:06 +0900 Subject: [PATCH 09/18] =?UTF-8?q?B114=20PAIZA=E9=81=8B=E5=8B=95=E4=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B084_RecommendationStore.java | 140 ------------------- knoda/src/B114_SportsDay.java | 129 +++++++++++++++++ knoda/test/B084_RecommendationStoreTest.java | 83 ----------- knoda/test/B114_SportsDayTest.java | 46 ++++++ 4 files changed, 175 insertions(+), 223 deletions(-) delete mode 100644 knoda/src/B084_RecommendationStore.java create mode 100644 knoda/src/B114_SportsDay.java delete mode 100644 knoda/test/B084_RecommendationStoreTest.java create mode 100644 knoda/test/B114_SportsDayTest.java diff --git a/knoda/src/B084_RecommendationStore.java b/knoda/src/B084_RecommendationStore.java deleted file mode 100644 index ee69a37..0000000 --- a/knoda/src/B084_RecommendationStore.java +++ /dev/null @@ -1,140 +0,0 @@ -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); - - /* - * 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(); - for (int store = 0; store < storeNum; store++) { - userEvaluation.add(scan.nextInt()); - } - otherEvaluation.add(userEvaluation); - } - - RecommendationStore recommendationStore = new RecommendationStore(); - // おすすめのお店を取得する - final Set storeSet = recommendationStore.getRecommendStore(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; // 評価が3のとき - private static final int ZERO_EVALUATION = 0; // 評価が0のとき - - // おすすめのお店を取得するメソッド - public Set getRecommendStore(final List oneselfEvaluation, final List> otherEvaluation, - final int criterionNum) { - - /* - * 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) { - // 自分と好みが似ているユーザが1人以上存在するとき - - /* - * zeroEvaluationStore : 自分が0と評価したお店を格納したリスト - * recommendStore : おすすめのお店を格納したセット - */ - final List zeroEvaluationStore = getEvaluationStore(oneselfEvaluation, ZERO_EVALUATION); - final Set recommendStore = new TreeSet(); - - // おすすめのお店を算出する - for (Integer user : resembleUser) { - for (Integer store : zeroEvaluationStore) { - if (otherEvaluation.get(user).get(store) == MAX_EVALUATION) { - recommendStore.add(store); - } - } - } - - if (recommendStore.size() == 0) { - // おすすめのお店が存在しないとき - return null; - } - - return recommendStore; - } - return null; - } - - // 自分が評価したお店を取得する - 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) == evaluation) { - evaluationStore.add(store); - } - } - return evaluationStore; - } -} diff --git a/knoda/src/B114_SportsDay.java b/knoda/src/B114_SportsDay.java new file mode 100644 index 0000000..5df00da --- /dev/null +++ b/knoda/src/B114_SportsDay.java @@ -0,0 +1,129 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Scanner; + +public class B114_SportsDay { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + /* + * playerNum : プレイヤーの人数 + * playNum : 試技回数 + */ + int playerNum = scan.nextInt(); + int playNum = scan.nextInt(); + + /* + * playerList : すべてのプレイヤーの記録を格納したリスト + * recordList : あるプレイヤーの記録を格納したリスト + */ + // すべてのプレイヤーの記録を入力する + final List> playerList = new ArrayList>(); + for (int player = 0; player < playerNum; player++) { + final List recordList = new ArrayList(); + for (int play = 0; play < playNum; play++) { + recordList.add(scan.nextInt()); + } + playerList.add(recordList); + } + + /* + * bestPlayerList : 最も良い記録を出したプレイヤーを格納したリスト + */ + SportsDay sportsDay = new SportsDay(); + // 最も良い記録を出したプレイヤーたちを取得する + final List bestPlayerList = sportsDay.getBestPlayer(playerList); + for (Integer player : bestPlayerList) { + // 出力の関係上、playerの値に1を足す + System.out.println(player + 1); + } + scan.close(); + } + +} + +// 運動会クラス +class SportsDay { + /* + * bestPlayerList : 最も良い記録を出したプレイヤーを格納したリスト + * INITIAL_VALUE : 初期値 + */ + private final List bestPlayerList = new ArrayList(); + private static final int INITIAL_VALUE = 0; + + // 最も良い記録を出したプレイヤーたちを取得するメソッド + public List getBestPlayer(final List> playerList) { + /* + * sortRecordList : プレイヤーごとの記録を降順に並び変えたリスト + */ + final List> sortRecordList = new ArrayList>(); + for (List recordList : playerList) { + Collections.sort(recordList, new SortList()); + sortRecordList.add(recordList); + } + /* + * play : 試技 + * playBestPlayer : k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、最も良い記録のプレイヤー + */ + int play = INITIAL_VALUE; + int playBestPlayer = INITIAL_VALUE; + + // 最も良い記録を出したプレイヤーを初期化する + setUp(playerList.size()); + + // 最も良い記録を出したプレイヤーが1人、もしくは試技回数に達するまで繰り返す + while ((bestPlayerList.size() != 1) && (play < playerList.get(INITIAL_VALUE).size())) { + /* + * playBestRecord : k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、最も良い記録 + * notBestPlayerList : k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、1番に良い記録でなかったプレイヤーを格納したリスト + */ + int playBestRecord = sortRecordList.get(bestPlayerList.get(INITIAL_VALUE)).get(play); + final List notBestPlayerList = new ArrayList(); + + // k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、最も良い記録を出したプレイヤーを算出する + for (Integer player : bestPlayerList) { + if (playBestRecord < sortRecordList.get(player).get(play)) { + playBestRecord = sortRecordList.get(player).get(play); + notBestPlayerList.add(playBestPlayer); + playBestPlayer = player; + } else if (playBestRecord > sortRecordList.get(player).get(play)) { + notBestPlayerList.add(player); + } + } + + // 1番に良い記録でなかったプレイヤーを削除する + for (Integer player : notBestPlayerList) { + bestPlayerList.remove(player); + } + play++; + } + return bestPlayerList; + } + + // 最も良い記録を出したプレイヤーを初期化するメソッド + private void setUp(final int playerNum) { + for (int player = 0; player < playerNum; player++) { + bestPlayerList.add(player); + } + } +} + +// リストを降順にソートするクラス +class SortList implements Comparator { + + @Override + public int compare(Integer record1, Integer record2) { + if (record1 > record2) { + return -1; + } else if (record1 < record2) { + return 1; + } else { + return 0; + } + } +} diff --git a/knoda/test/B084_RecommendationStoreTest.java b/knoda/test/B084_RecommendationStoreTest.java deleted file mode 100644 index 372c0b5..0000000 --- a/knoda/test/B084_RecommendationStoreTest.java +++ /dev/null @@ -1,83 +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 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.getRecommendStore(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.getRecommendStore(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.getRecommendStore(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.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); - Set expected = null; - assertThat(actual, is(expected)); - } - -} diff --git a/knoda/test/B114_SportsDayTest.java b/knoda/test/B114_SportsDayTest.java new file mode 100644 index 0000000..d529730 --- /dev/null +++ b/knoda/test/B114_SportsDayTest.java @@ -0,0 +1,46 @@ +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 B114_SportsDayTest { + List> playerList; + SportsDay sut; + + @Test + public void 最も良い記録を出したプレイヤーが1人のとき() { + playerList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(400, 450, 420)), + new ArrayList(Arrays.asList(500, 400, 410)), + new ArrayList(Arrays.asList(490, 500, 490)) + )); + sut = new SportsDay(); + + int expected = 3; + List actualList = sut.getBestPlayer(playerList); + assertThat(actualList.get(0) + 1, is(expected)); + } + + @Test + public void 最も良い記録を出したプレイヤーが複数のとき() { + playerList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(300, 310, 440, 430)), + new ArrayList(Arrays.asList(410, 420, 430, 440)), + new ArrayList(Arrays.asList(430, 420, 440, 410)) + )); + sut = new SportsDay(); + + List expectedList = new ArrayList(Arrays.asList(2, 3)); + List actualList = sut.getBestPlayer(playerList); + for (int i = 0; i < expectedList.size(); i++) { + assertThat(actualList.get(i) + 1, is(expectedList.get(i))); + } + } + +} -- GitLab From 0a9b7630d5e9e37cb4bc6c70fc0f2bf2bb5499cc 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: Tue, 26 Jul 2022 15:12:38 +0900 Subject: [PATCH 10/18] =?UTF-8?q?B114=20PAIZA=E9=81=8B=E5=8B=95=E4=BC=9A?= =?UTF-8?q?=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/B114_SportsDay.java | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/knoda/src/B114_SportsDay.java b/knoda/src/B114_SportsDay.java index 5df00da..66cd21d 100644 --- a/knoda/src/B114_SportsDay.java +++ b/knoda/src/B114_SportsDay.java @@ -37,7 +37,7 @@ public class B114_SportsDay { */ SportsDay sportsDay = new SportsDay(); // 最も良い記録を出したプレイヤーたちを取得する - final List bestPlayerList = sportsDay.getBestPlayer(playerList); + final List bestPlayerList = sportsDay.getBestPlayer(playerList, playNum); for (Integer player : bestPlayerList) { // 出力の関係上、playerの値に1を足す System.out.println(player + 1); @@ -57,13 +57,17 @@ class SportsDay { private static final int INITIAL_VALUE = 0; // 最も良い記録を出したプレイヤーたちを取得するメソッド - public List getBestPlayer(final List> playerList) { + public List getBestPlayer(final List> playerList, final int playNum) { /* * sortRecordList : プレイヤーごとの記録を降順に並び変えたリスト */ final List> sortRecordList = new ArrayList>(); for (List recordList : playerList) { - Collections.sort(recordList, new SortList()); + // Collections.sort(recordList, new SortList()); // Comparatorを使用したとき + + // Collections.sortとCollections.reverseを使用したとき + Collections.sort(recordList); + Collections.reverse(recordList); sortRecordList.add(recordList); } /* @@ -77,7 +81,7 @@ class SportsDay { setUp(playerList.size()); // 最も良い記録を出したプレイヤーが1人、もしくは試技回数に達するまで繰り返す - while ((bestPlayerList.size() != 1) && (play < playerList.get(INITIAL_VALUE).size())) { + while ((bestPlayerList.size() != 1) && (play < playNum)) { /* * playBestRecord : k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、最も良い記録 * notBestPlayerList : k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、1番に良い記録でなかったプレイヤーを格納したリスト @@ -114,16 +118,13 @@ class SportsDay { } // リストを降順にソートするクラス + class SortList implements Comparator { @Override public int compare(Integer record1, Integer record2) { - if (record1 > record2) { - return -1; - } else if (record1 < record2) { - return 1; - } else { - return 0; - } + return record2 - record1; } } + + -- GitLab From 6df5e26ebb8f20386c269c1451f008b3cf1f22cc 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: Wed, 27 Jul 2022 13:34:56 +0900 Subject: [PATCH 11/18] =?UTF-8?q?B085=20=E3=81=8A=E5=AE=9D=E3=81=95?= =?UTF-8?q?=E3=81=8C=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B085_TreasureHunt.java | 128 +++++++++++++++++++++++++ knoda/src/B114_SportsDay.java | 130 -------------------------- knoda/test/B085_TreasureHuntTest.java | 50 ++++++++++ knoda/test/B114_SportsDayTest.java | 46 --------- 4 files changed, 178 insertions(+), 176 deletions(-) create mode 100644 knoda/src/B085_TreasureHunt.java delete mode 100644 knoda/src/B114_SportsDay.java create mode 100644 knoda/test/B085_TreasureHuntTest.java delete mode 100644 knoda/test/B114_SportsDayTest.java diff --git a/knoda/src/B085_TreasureHunt.java b/knoda/src/B085_TreasureHunt.java new file mode 100644 index 0000000..e7bfc35 --- /dev/null +++ b/knoda/src/B085_TreasureHunt.java @@ -0,0 +1,128 @@ +package hellojunit; + +import java.util.Scanner; + +public class B085_TreasureHunt { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 宝が存在している位置を入力する + int treasurePosition = scan.nextInt(); + + TreasureHunt treasureHunt = new TreasureHunt(treasurePosition); + // 1つ目の方法(getTravelDistance1)と2つ目の方法(getTravelDistance2)でそれぞれ移動距離を取得する + final int travelDistance1 = treasureHunt.getTravelDistance1(); + final int travelDistance2 = treasureHunt.getTravelDistance2(); + System.out.println(travelDistance1 + " " + travelDistance2); + scan.close(); + } + +} + +// 移動距離を取得するクラス +class TreasureHunt { + private static final int START_POSITION_INIT = 0; // 原点(スタート位置) + private static final int FINISH_POSITION_INIT = 1; // 最初に移動するとき、移動終了の位置 + private static final int TRAVEL_DISTANCE_INIT = 0; // 移動距離の初期化 + private final int treasurePosition; // 宝が存在している座標 + private boolean isTreasure; // 宝が見つかったかの判定(true : 見つかった、false : 見つかっていない) + private int startPosition; // startPosition : 移動開始の位置 + private int finishPosition; // finishPosition : 移動終了の位置 + private int travelDistance; // travelDistance : 移動距離 + + public TreasureHunt(final int treasurePosition) { + this.treasurePosition = treasurePosition; + } + + // 移動開始位置、移動終了位置、移動距離の初期化 + private void setUp(final int startPosition, final int finishPosition, + final int travelDistance, final boolean isTreasure) { + this.startPosition = startPosition; + this.finishPosition = finishPosition; + this.travelDistance = travelDistance; + this.isTreasure = isTreasure; + } + + // 1つ目の方法で移動距離を取得するメソッド + public int getTravelDistance1() { + // 移動開始位置、移動終了位置、移動距離の初期化 + setUp(START_POSITION_INIT, FINISH_POSITION_INIT, TRAVEL_DISTANCE_INIT, false); + while (true) { + + // 移動距離を取得する + travelDistance = calcTravelDistance(startPosition, finishPosition, travelDistance); + + // startPosition、FinishPositionを更新する + if (startPosition < finishPosition) { + int tmpPosition = startPosition; + startPosition = finishPosition; + finishPosition = startPosition - (finishPosition - tmpPosition) - 1; + } else { + int tmpPosition = startPosition; + startPosition = finishPosition; + finishPosition = startPosition + (tmpPosition - finishPosition) + 1; + } + + // 宝が見つかったとき + if (isTreasure) { + break; + } + + } + return travelDistance; + } + + // 2つ目の方法で移動距離を取得するメソッド + public int getTravelDistance2() { + setUp(START_POSITION_INIT, FINISH_POSITION_INIT, TRAVEL_DISTANCE_INIT, false); + while (true) { + + // 移動距離を取得する + travelDistance = calcTravelDistance(startPosition, finishPosition, travelDistance); + + // startPosition、FinishPositionを更新する + if (startPosition < finishPosition) { + int tmpPosition = startPosition; + startPosition = finishPosition; + if (finishPosition == FINISH_POSITION_INIT) { + finishPosition = startPosition - (finishPosition - tmpPosition) - 1; + } else { + finishPosition = startPosition - (finishPosition - tmpPosition) - finishPosition / 2; + } + } else { + int tmpPosition = startPosition; + startPosition = finishPosition; + finishPosition = startPosition + (tmpPosition - finishPosition) - finishPosition; + } + + // 宝が見つかったとき + if (isTreasure) { + break; + } + } + return travelDistance; + } + + // 移動距離を算出するメソッド + private int calcTravelDistance(final int startPosition, final int finishPosition, int travelDistance) { + int position = startPosition; + + // 宝が見つかる、もしくは移動終了位置まで到達するまで移動距離をカウントアップする + while ((position != treasurePosition) && (position != finishPosition)) { + travelDistance++; + if (startPosition < finishPosition) { + position++; + } else { + position--; + } + } + + // 宝が見つかったとき + if (position == treasurePosition) { + isTreasure = true; + } + return travelDistance; + } + + +} diff --git a/knoda/src/B114_SportsDay.java b/knoda/src/B114_SportsDay.java deleted file mode 100644 index 66cd21d..0000000 --- a/knoda/src/B114_SportsDay.java +++ /dev/null @@ -1,130 +0,0 @@ -package hellojunit; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Scanner; - -public class B114_SportsDay { - - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - - /* - * playerNum : プレイヤーの人数 - * playNum : 試技回数 - */ - int playerNum = scan.nextInt(); - int playNum = scan.nextInt(); - - /* - * playerList : すべてのプレイヤーの記録を格納したリスト - * recordList : あるプレイヤーの記録を格納したリスト - */ - // すべてのプレイヤーの記録を入力する - final List> playerList = new ArrayList>(); - for (int player = 0; player < playerNum; player++) { - final List recordList = new ArrayList(); - for (int play = 0; play < playNum; play++) { - recordList.add(scan.nextInt()); - } - playerList.add(recordList); - } - - /* - * bestPlayerList : 最も良い記録を出したプレイヤーを格納したリスト - */ - SportsDay sportsDay = new SportsDay(); - // 最も良い記録を出したプレイヤーたちを取得する - final List bestPlayerList = sportsDay.getBestPlayer(playerList, playNum); - for (Integer player : bestPlayerList) { - // 出力の関係上、playerの値に1を足す - System.out.println(player + 1); - } - scan.close(); - } - -} - -// 運動会クラス -class SportsDay { - /* - * bestPlayerList : 最も良い記録を出したプレイヤーを格納したリスト - * INITIAL_VALUE : 初期値 - */ - private final List bestPlayerList = new ArrayList(); - private static final int INITIAL_VALUE = 0; - - // 最も良い記録を出したプレイヤーたちを取得するメソッド - public List getBestPlayer(final List> playerList, final int playNum) { - /* - * sortRecordList : プレイヤーごとの記録を降順に並び変えたリスト - */ - final List> sortRecordList = new ArrayList>(); - for (List recordList : playerList) { - // Collections.sort(recordList, new SortList()); // Comparatorを使用したとき - - // Collections.sortとCollections.reverseを使用したとき - Collections.sort(recordList); - Collections.reverse(recordList); - sortRecordList.add(recordList); - } - /* - * play : 試技 - * playBestPlayer : k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、最も良い記録のプレイヤー - */ - int play = INITIAL_VALUE; - int playBestPlayer = INITIAL_VALUE; - - // 最も良い記録を出したプレイヤーを初期化する - setUp(playerList.size()); - - // 最も良い記録を出したプレイヤーが1人、もしくは試技回数に達するまで繰り返す - while ((bestPlayerList.size() != 1) && (play < playNum)) { - /* - * playBestRecord : k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、最も良い記録 - * notBestPlayerList : k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、1番に良い記録でなかったプレイヤーを格納したリスト - */ - int playBestRecord = sortRecordList.get(bestPlayerList.get(INITIAL_VALUE)).get(play); - final List notBestPlayerList = new ArrayList(); - - // k番目にいい記録を出したプレイヤーたち(bestPlayerList)の中で、最も良い記録を出したプレイヤーを算出する - for (Integer player : bestPlayerList) { - if (playBestRecord < sortRecordList.get(player).get(play)) { - playBestRecord = sortRecordList.get(player).get(play); - notBestPlayerList.add(playBestPlayer); - playBestPlayer = player; - } else if (playBestRecord > sortRecordList.get(player).get(play)) { - notBestPlayerList.add(player); - } - } - - // 1番に良い記録でなかったプレイヤーを削除する - for (Integer player : notBestPlayerList) { - bestPlayerList.remove(player); - } - play++; - } - return bestPlayerList; - } - - // 最も良い記録を出したプレイヤーを初期化するメソッド - private void setUp(final int playerNum) { - for (int player = 0; player < playerNum; player++) { - bestPlayerList.add(player); - } - } -} - -// リストを降順にソートするクラス - -class SortList implements Comparator { - - @Override - public int compare(Integer record1, Integer record2) { - return record2 - record1; - } -} - - diff --git a/knoda/test/B085_TreasureHuntTest.java b/knoda/test/B085_TreasureHuntTest.java new file mode 100644 index 0000000..33922bd --- /dev/null +++ b/knoda/test/B085_TreasureHuntTest.java @@ -0,0 +1,50 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class B085_TreasureHuntTest { + + int treasurePosition; + TreasureHunt sut; + @Test + public void 宝の座標がマイナス3のとき21_23が出力されるテスト() { + treasurePosition = -3; + sut = new TreasureHunt(treasurePosition); + int expected1 = 21; + int expected2 = 23; + int actual1 = sut.getTravelDistance1(); + int actual2 = sut.getTravelDistance2(); + assertThat(actual1, is(expected1)); + assertThat(actual2, is(expected2)); + } + + @Test + public void 宝の座標が5のとき45_33が出力されるテスト() { + treasurePosition = 5; + sut = new TreasureHunt(treasurePosition); + int expected1 = 45; + int expected2 = 33; + int actual1 = sut.getTravelDistance1(); + int actual2 = sut.getTravelDistance2(); + assertThat(actual1, is(expected1)); + assertThat(actual2, is(expected2)); + } + + @Test + public void 宝の座標が0のとき0_0が出力されるテスト() { + treasurePosition = 0; + sut = new TreasureHunt(treasurePosition); + int expected1 = 0; + int expected2 = 0; + int actual1 = sut.getTravelDistance1(); + int actual2 = sut.getTravelDistance2(); + assertThat(actual1, is(expected1)); + assertThat(actual2, is(expected2)); + } + + + +} diff --git a/knoda/test/B114_SportsDayTest.java b/knoda/test/B114_SportsDayTest.java deleted file mode 100644 index d529730..0000000 --- a/knoda/test/B114_SportsDayTest.java +++ /dev/null @@ -1,46 +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 B114_SportsDayTest { - List> playerList; - SportsDay sut; - - @Test - public void 最も良い記録を出したプレイヤーが1人のとき() { - playerList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(400, 450, 420)), - new ArrayList(Arrays.asList(500, 400, 410)), - new ArrayList(Arrays.asList(490, 500, 490)) - )); - sut = new SportsDay(); - - int expected = 3; - List actualList = sut.getBestPlayer(playerList); - assertThat(actualList.get(0) + 1, is(expected)); - } - - @Test - public void 最も良い記録を出したプレイヤーが複数のとき() { - playerList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(300, 310, 440, 430)), - new ArrayList(Arrays.asList(410, 420, 430, 440)), - new ArrayList(Arrays.asList(430, 420, 440, 410)) - )); - sut = new SportsDay(); - - List expectedList = new ArrayList(Arrays.asList(2, 3)); - List actualList = sut.getBestPlayer(playerList); - for (int i = 0; i < expectedList.size(); i++) { - assertThat(actualList.get(i) + 1, is(expectedList.get(i))); - } - } - -} -- GitLab From 2f67338a3aff0b87f1331971a187ce3cae67b0c1 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: Wed, 27 Jul 2022 16:09:52 +0900 Subject: [PATCH 12/18] =?UTF-8?q?B085=20=E3=81=8A=E5=AE=9D=E3=81=95?= =?UTF-8?q?=E3=81=8C=E3=81=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/B085_TreasureHunt.java | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/knoda/src/B085_TreasureHunt.java b/knoda/src/B085_TreasureHunt.java index e7bfc35..48863ce 100644 --- a/knoda/src/B085_TreasureHunt.java +++ b/knoda/src/B085_TreasureHunt.java @@ -7,7 +7,7 @@ public class B085_TreasureHunt { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 宝が存在している位置を入力する - int treasurePosition = scan.nextInt(); + final int treasurePosition = scan.nextInt(); TreasureHunt treasureHunt = new TreasureHunt(treasurePosition); // 1つ目の方法(getTravelDistance1)と2つ目の方法(getTravelDistance2)でそれぞれ移動距離を取得する @@ -26,27 +26,34 @@ class TreasureHunt { private static final int TRAVEL_DISTANCE_INIT = 0; // 移動距離の初期化 private final int treasurePosition; // 宝が存在している座標 private boolean isTreasure; // 宝が見つかったかの判定(true : 見つかった、false : 見つかっていない) - private int startPosition; // startPosition : 移動開始の位置 - private int finishPosition; // finishPosition : 移動終了の位置 - private int travelDistance; // travelDistance : 移動距離 + // private int startPosition; // startPosition : 移動開始の位置 + // private int finishPosition; // finishPosition : 移動終了の位置 + // private int travelDistance; // travelDistance : 移動距離 public TreasureHunt(final int treasurePosition) { this.treasurePosition = treasurePosition; } + /* // 移動開始位置、移動終了位置、移動距離の初期化 - private void setUp(final int startPosition, final int finishPosition, + private void init(final int startPosition, final int finishPosition, final int travelDistance, final boolean isTreasure) { this.startPosition = startPosition; this.finishPosition = finishPosition; this.travelDistance = travelDistance; this.isTreasure = isTreasure; } + */ // 1つ目の方法で移動距離を取得するメソッド public int getTravelDistance1() { // 移動開始位置、移動終了位置、移動距離の初期化 - setUp(START_POSITION_INIT, FINISH_POSITION_INIT, TRAVEL_DISTANCE_INIT, false); + // init(START_POSITION_INIT, FINISH_POSITION_INIT, TRAVEL_DISTANCE_INIT, false); + + isTreasure = false; // 宝が見つかったかの判定(true : 見つかった、false : 見つかっていない) + int startPosition = START_POSITION_INIT; // startPosition : 移動開始の位置 + int finishPosition = FINISH_POSITION_INIT; // finishPosition : 移動終了の位置 + int travelDistance = TRAVEL_DISTANCE_INIT; // travelDistance : 移動距離 while (true) { // 移動距離を取得する @@ -74,7 +81,12 @@ class TreasureHunt { // 2つ目の方法で移動距離を取得するメソッド public int getTravelDistance2() { - setUp(START_POSITION_INIT, FINISH_POSITION_INIT, TRAVEL_DISTANCE_INIT, false); + // init(START_POSITION_INIT, FINISH_POSITION_INIT, TRAVEL_DISTANCE_INIT, false); + + isTreasure = false; // 宝が見つかったかの判定(true : 見つかった、false : 見つかっていない) + int startPosition = START_POSITION_INIT; // startPosition : 移動開始の位置 + int finishPosition = FINISH_POSITION_INIT; // finishPosition : 移動終了の位置 + int travelDistance = TRAVEL_DISTANCE_INIT; // travelDistance : 移動距離 while (true) { // 移動距離を取得する @@ -82,7 +94,7 @@ class TreasureHunt { // startPosition、FinishPositionを更新する if (startPosition < finishPosition) { - int tmpPosition = startPosition; + final int tmpPosition = startPosition; startPosition = finishPosition; if (finishPosition == FINISH_POSITION_INIT) { finishPosition = startPosition - (finishPosition - tmpPosition) - 1; @@ -90,7 +102,7 @@ class TreasureHunt { finishPosition = startPosition - (finishPosition - tmpPosition) - finishPosition / 2; } } else { - int tmpPosition = startPosition; + final int tmpPosition = startPosition; startPosition = finishPosition; finishPosition = startPosition + (tmpPosition - finishPosition) - finishPosition; } -- GitLab From 0d851254d88d92d4b48a203e9097a372b5fd29a2 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, 28 Jul 2022 10:29:40 +0900 Subject: [PATCH 13/18] =?UTF-8?q?B089=20=E7=A7=98=E5=AF=86=E3=81=AE?= =?UTF-8?q?=E8=A8=80=E8=91=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B085_TreasureHunt.java | 140 -------------------------- knoda/src/B089_SecretWord.java | 104 +++++++++++++++++++ knoda/test/B085_TreasureHuntTest.java | 50 --------- knoda/test/B089_SecretWordTest.java | 98 ++++++++++++++++++ 4 files changed, 202 insertions(+), 190 deletions(-) delete mode 100644 knoda/src/B085_TreasureHunt.java create mode 100644 knoda/src/B089_SecretWord.java delete mode 100644 knoda/test/B085_TreasureHuntTest.java create mode 100644 knoda/test/B089_SecretWordTest.java diff --git a/knoda/src/B085_TreasureHunt.java b/knoda/src/B085_TreasureHunt.java deleted file mode 100644 index 48863ce..0000000 --- a/knoda/src/B085_TreasureHunt.java +++ /dev/null @@ -1,140 +0,0 @@ -package hellojunit; - -import java.util.Scanner; - -public class B085_TreasureHunt { - - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - // 宝が存在している位置を入力する - final int treasurePosition = scan.nextInt(); - - TreasureHunt treasureHunt = new TreasureHunt(treasurePosition); - // 1つ目の方法(getTravelDistance1)と2つ目の方法(getTravelDistance2)でそれぞれ移動距離を取得する - final int travelDistance1 = treasureHunt.getTravelDistance1(); - final int travelDistance2 = treasureHunt.getTravelDistance2(); - System.out.println(travelDistance1 + " " + travelDistance2); - scan.close(); - } - -} - -// 移動距離を取得するクラス -class TreasureHunt { - private static final int START_POSITION_INIT = 0; // 原点(スタート位置) - private static final int FINISH_POSITION_INIT = 1; // 最初に移動するとき、移動終了の位置 - private static final int TRAVEL_DISTANCE_INIT = 0; // 移動距離の初期化 - private final int treasurePosition; // 宝が存在している座標 - private boolean isTreasure; // 宝が見つかったかの判定(true : 見つかった、false : 見つかっていない) - // private int startPosition; // startPosition : 移動開始の位置 - // private int finishPosition; // finishPosition : 移動終了の位置 - // private int travelDistance; // travelDistance : 移動距離 - - public TreasureHunt(final int treasurePosition) { - this.treasurePosition = treasurePosition; - } - - /* - // 移動開始位置、移動終了位置、移動距離の初期化 - private void init(final int startPosition, final int finishPosition, - final int travelDistance, final boolean isTreasure) { - this.startPosition = startPosition; - this.finishPosition = finishPosition; - this.travelDistance = travelDistance; - this.isTreasure = isTreasure; - } - */ - - // 1つ目の方法で移動距離を取得するメソッド - public int getTravelDistance1() { - // 移動開始位置、移動終了位置、移動距離の初期化 - // init(START_POSITION_INIT, FINISH_POSITION_INIT, TRAVEL_DISTANCE_INIT, false); - - isTreasure = false; // 宝が見つかったかの判定(true : 見つかった、false : 見つかっていない) - int startPosition = START_POSITION_INIT; // startPosition : 移動開始の位置 - int finishPosition = FINISH_POSITION_INIT; // finishPosition : 移動終了の位置 - int travelDistance = TRAVEL_DISTANCE_INIT; // travelDistance : 移動距離 - while (true) { - - // 移動距離を取得する - travelDistance = calcTravelDistance(startPosition, finishPosition, travelDistance); - - // startPosition、FinishPositionを更新する - if (startPosition < finishPosition) { - int tmpPosition = startPosition; - startPosition = finishPosition; - finishPosition = startPosition - (finishPosition - tmpPosition) - 1; - } else { - int tmpPosition = startPosition; - startPosition = finishPosition; - finishPosition = startPosition + (tmpPosition - finishPosition) + 1; - } - - // 宝が見つかったとき - if (isTreasure) { - break; - } - - } - return travelDistance; - } - - // 2つ目の方法で移動距離を取得するメソッド - public int getTravelDistance2() { - // init(START_POSITION_INIT, FINISH_POSITION_INIT, TRAVEL_DISTANCE_INIT, false); - - isTreasure = false; // 宝が見つかったかの判定(true : 見つかった、false : 見つかっていない) - int startPosition = START_POSITION_INIT; // startPosition : 移動開始の位置 - int finishPosition = FINISH_POSITION_INIT; // finishPosition : 移動終了の位置 - int travelDistance = TRAVEL_DISTANCE_INIT; // travelDistance : 移動距離 - while (true) { - - // 移動距離を取得する - travelDistance = calcTravelDistance(startPosition, finishPosition, travelDistance); - - // startPosition、FinishPositionを更新する - if (startPosition < finishPosition) { - final int tmpPosition = startPosition; - startPosition = finishPosition; - if (finishPosition == FINISH_POSITION_INIT) { - finishPosition = startPosition - (finishPosition - tmpPosition) - 1; - } else { - finishPosition = startPosition - (finishPosition - tmpPosition) - finishPosition / 2; - } - } else { - final int tmpPosition = startPosition; - startPosition = finishPosition; - finishPosition = startPosition + (tmpPosition - finishPosition) - finishPosition; - } - - // 宝が見つかったとき - if (isTreasure) { - break; - } - } - return travelDistance; - } - - // 移動距離を算出するメソッド - private int calcTravelDistance(final int startPosition, final int finishPosition, int travelDistance) { - int position = startPosition; - - // 宝が見つかる、もしくは移動終了位置まで到達するまで移動距離をカウントアップする - while ((position != treasurePosition) && (position != finishPosition)) { - travelDistance++; - if (startPosition < finishPosition) { - position++; - } else { - position--; - } - } - - // 宝が見つかったとき - if (position == treasurePosition) { - isTreasure = true; - } - return travelDistance; - } - - -} diff --git a/knoda/src/B089_SecretWord.java b/knoda/src/B089_SecretWord.java new file mode 100644 index 0000000..c839842 --- /dev/null +++ b/knoda/src/B089_SecretWord.java @@ -0,0 +1,104 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B089_SecretWord { + + public static void main(String[] args) { + final int INDEX_ZERO = 0; // 0番目のインデックス + final int INDEX_ONE = 1; // 1番目のインデックス + + Scanner scan = new Scanner(System.in); + + final int boardSize = scan.nextInt(); // boardSize : 基盤のサイズ + final int wordNum = scan.nextInt(); // wordNum : 単語の数 + final List characterList = new ArrayList(); // characterList : 基盤に記載されている文字 + + // 基盤に記載されている文字を入力する + for (int i = 0; i < boardSize; i++) { + characterList.add(scan.next()); + } + + // 単語を入力する + final List wordList = new ArrayList(); + for (int i = 0; i < wordNum; i++) { + wordList.add(scan.next()); + } + + + SecretWord secretWord = new SecretWord(); + + // 単語の先頭文字の位置を取得する + try { + final List> rowAndColumnList = + secretWord.getColumnAndRowInWord(characterList, wordList, boardSize); + // 単語の先頭文字の位置を出力する + for (List rowAndColumn : rowAndColumnList) { + System.out.print((rowAndColumn.get(INDEX_ZERO) + 1) + " "); + System.out.println(rowAndColumn.get(INDEX_ONE) + 1); + } + } catch (NullPointerException e) { + System.out.println("単語が基盤に存在していません"); + } + scan.close(); + } + +} + +// 単語の先頭文字の位置を取得するクラス +class SecretWord { + + // それぞれの単語の先頭文字の位置を取得するメソッド + public List> getColumnAndRowInWord(final List characterList, final List wordList, + final int boardSize) { + // columnAndRowList : それぞれの単語の先頭文字の位置を格納するリスト + final List> columnAndRowList = new ArrayList>(); + + // それぞれの単語の先頭文字の位置を取得する + for (String word : wordList) { + // columnAndRow : ある単語の先頭文字の位置を格納するリスト + // ある単語の先頭文字の位置を取得する + List columnAndRow = getColumnAndRow(characterList, word, boardSize); + columnAndRowList.add(columnAndRow); + } + return columnAndRowList; + } + + // ある単語の先頭文字の位置を取得するメソッド + private List getColumnAndRow(final List characterList, final String word, + final int boardSize) { + + // 基盤に書かれている文字を1文字ずつ走査する + for (int row = 0; row < boardSize - word.length() + 1; row++) { + for (int column = 0; column < boardSize - word.length() + 1; column++) { + int tmpRow = row; + int tmpColumn = column; + int wordIndex = 0; // wordIndex : 単語の文字の要素番号 + + // 単語が基盤に存在しているか調べる + while (wordIndex < word.length()) { + if (characterList.get(tmpRow).charAt(tmpColumn) == word.charAt(wordIndex)) { + tmpRow++; + tmpColumn++; + wordIndex++; + } else { + break; + } + } + + // columnAndRow : ある単語の先頭文字の位置を格納するリスト + final List columnAndRow = new ArrayList(); + + // 単語が基盤に存在しているとき + if (wordIndex == word.length()) { + columnAndRow.add(column); + columnAndRow.add(row); + return columnAndRow; + } + } + } + return null; + } +} diff --git a/knoda/test/B085_TreasureHuntTest.java b/knoda/test/B085_TreasureHuntTest.java deleted file mode 100644 index 33922bd..0000000 --- a/knoda/test/B085_TreasureHuntTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package hellojunit; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - -import org.junit.Test; - -public class B085_TreasureHuntTest { - - int treasurePosition; - TreasureHunt sut; - @Test - public void 宝の座標がマイナス3のとき21_23が出力されるテスト() { - treasurePosition = -3; - sut = new TreasureHunt(treasurePosition); - int expected1 = 21; - int expected2 = 23; - int actual1 = sut.getTravelDistance1(); - int actual2 = sut.getTravelDistance2(); - assertThat(actual1, is(expected1)); - assertThat(actual2, is(expected2)); - } - - @Test - public void 宝の座標が5のとき45_33が出力されるテスト() { - treasurePosition = 5; - sut = new TreasureHunt(treasurePosition); - int expected1 = 45; - int expected2 = 33; - int actual1 = sut.getTravelDistance1(); - int actual2 = sut.getTravelDistance2(); - assertThat(actual1, is(expected1)); - assertThat(actual2, is(expected2)); - } - - @Test - public void 宝の座標が0のとき0_0が出力されるテスト() { - treasurePosition = 0; - sut = new TreasureHunt(treasurePosition); - int expected1 = 0; - int expected2 = 0; - int actual1 = sut.getTravelDistance1(); - int actual2 = sut.getTravelDistance2(); - assertThat(actual1, is(expected1)); - assertThat(actual2, is(expected2)); - } - - - -} diff --git a/knoda/test/B089_SecretWordTest.java b/knoda/test/B089_SecretWordTest.java new file mode 100644 index 0000000..8dc7be8 --- /dev/null +++ b/knoda/test/B089_SecretWordTest.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 B089_SecretWordTest { + List characterList; + List wordList; + int boardSize; + SecretWord sut; + + @Test + public void 基盤に単語が存在しているテスト1() { + characterList = new ArrayList(); + wordList = new ArrayList(); + boardSize = 6; + + characterList.add("HPPLLM"); + characterList.add("UROQUV"); + characterList.add("FBSRZY"); + characterList.add("DPEFKT"); + characterList.add("GBBEUY"); + characterList.add("EMCQFY"); + + wordList.add("BEEF"); + wordList.add("PORK"); + + sut = new SecretWord(); + List> expectedList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(2, 3)), + new ArrayList(Arrays.asList(2, 1)))); + List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); + for (int i = 0; i < expectedList.size(); i++) { + for (int j = 0; j < expectedList.get(i).size(); j++) { + assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); + } + } + } + + @Test + public void 基盤に単語が存在しているテスト2() { + characterList = new ArrayList(); + wordList = new ArrayList(); + boardSize = 4; + + characterList.add("ACEG"); + characterList.add("HBDF"); + characterList.add("EGAC"); + characterList.add("DFHB"); + + wordList.add("ABA"); + wordList.add("BAB"); + + sut = new SecretWord(); + List> expectedList = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(1, 1)), + new ArrayList(Arrays.asList(2, 2)))); + List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); + for (int i = 0; i < expectedList.size(); i++) { + for (int j = 0; j < expectedList.get(i).size(); j++) { + assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); + } + } + } + + @Test(expected = NullPointerException.class) + public void 基盤に単語が存在していないテスト() { + characterList = new ArrayList(); + wordList = new ArrayList(); + boardSize = 4; + + characterList.add("ACEG"); + characterList.add("HBDF"); + characterList.add("EGAC"); + characterList.add("DFHB"); + + wordList.add("HAF"); + wordList.add("BAB"); + + sut = new SecretWord(); + List> expectedList = new ArrayList>(Arrays.asList( + new ArrayList(null), + new ArrayList(Arrays.asList(2, 2)))); + List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); + for (int i = 0; i < expectedList.size(); i++) { + for (int j = 0; j < expectedList.get(i).size(); j++) { + assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); + } + } + } + +} -- GitLab From 9f170b22918c360db116fcf14e67d9ffad40056f 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, 28 Jul 2022 17:05:30 +0900 Subject: [PATCH 14/18] =?UTF-8?q?B089=20=E7=A7=98=E5=AF=86=E3=81=AE?= =?UTF-8?q?=E8=A8=80=E8=91=89=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/B089_SecretWord.java | 33 ++++++++++++++++------------- knoda/test/B089_SecretWordTest.java | 4 ++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/knoda/src/B089_SecretWord.java b/knoda/src/B089_SecretWord.java index c839842..e1741ef 100644 --- a/knoda/src/B089_SecretWord.java +++ b/knoda/src/B089_SecretWord.java @@ -31,16 +31,12 @@ public class B089_SecretWord { SecretWord secretWord = new SecretWord(); // 単語の先頭文字の位置を取得する - try { - final List> rowAndColumnList = - secretWord.getColumnAndRowInWord(characterList, wordList, boardSize); - // 単語の先頭文字の位置を出力する - for (List rowAndColumn : rowAndColumnList) { - System.out.print((rowAndColumn.get(INDEX_ZERO) + 1) + " "); - System.out.println(rowAndColumn.get(INDEX_ONE) + 1); - } - } catch (NullPointerException e) { - System.out.println("単語が基盤に存在していません"); + final List> rowAndColumnList = + secretWord.getColumnAndRowInWord(characterList, wordList, boardSize); + // 単語の先頭文字の位置を出力する + for (List rowAndColumn : rowAndColumnList) { + System.out.print((rowAndColumn.get(INDEX_ZERO) + 1) + " "); + System.out.println(rowAndColumn.get(INDEX_ONE) + 1); } scan.close(); } @@ -70,6 +66,11 @@ class SecretWord { private List getColumnAndRow(final List characterList, final String word, final int boardSize) { + // columnAndRow : ある単語の先頭文字の位置を格納するリスト + final List columnAndRow = new ArrayList(); + + boolean isFindWord = false; // isFindWord : 単語が見つかったかの判定(true : 見つかった、false : 見つかっていない) + // 基盤に書かれている文字を1文字ずつ走査する for (int row = 0; row < boardSize - word.length() + 1; row++) { for (int column = 0; column < boardSize - word.length() + 1; column++) { @@ -88,17 +89,19 @@ class SecretWord { } } - // columnAndRow : ある単語の先頭文字の位置を格納するリスト - final List columnAndRow = new ArrayList(); - // 単語が基盤に存在しているとき if (wordIndex == word.length()) { columnAndRow.add(column); columnAndRow.add(row); - return columnAndRow; + isFindWord = true; } } + + // 単語が見つかったら終了 + if (isFindWord) { + break; + } } - return null; + return columnAndRow; } } diff --git a/knoda/test/B089_SecretWordTest.java b/knoda/test/B089_SecretWordTest.java index 8dc7be8..db2d734 100644 --- a/knoda/test/B089_SecretWordTest.java +++ b/knoda/test/B089_SecretWordTest.java @@ -69,7 +69,7 @@ public class B089_SecretWordTest { } } - @Test(expected = NullPointerException.class) + @Test public void 基盤に単語が存在していないテスト() { characterList = new ArrayList(); wordList = new ArrayList(); @@ -85,7 +85,7 @@ public class B089_SecretWordTest { sut = new SecretWord(); List> expectedList = new ArrayList>(Arrays.asList( - new ArrayList(null), + new ArrayList(), new ArrayList(Arrays.asList(2, 2)))); List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); for (int i = 0; i < expectedList.size(); i++) { -- GitLab From 55f548b5046ac933beea034ebae38d70bbafdebf 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, 29 Jul 2022 11:37:39 +0900 Subject: [PATCH 15/18] =?UTF-8?q?B050=20=E6=9C=89=E5=8A=B9=E3=81=AA?= =?UTF-8?q?=E3=83=81=E3=82=B1=E3=83=83=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B050_Ticket.java | 93 +++++++++++++++++++++ knoda/src/B089_SecretWord.java | 107 ------------------------ knoda/test/B050_TicketTest.java | 125 ++++++++++++++++++++++++++++ knoda/test/B089_SecretWordTest.java | 98 ---------------------- 4 files changed, 218 insertions(+), 205 deletions(-) create mode 100644 knoda/src/B050_Ticket.java delete mode 100644 knoda/src/B089_SecretWord.java create mode 100644 knoda/test/B050_TicketTest.java delete mode 100644 knoda/test/B089_SecretWordTest.java diff --git a/knoda/src/B050_Ticket.java b/knoda/src/B050_Ticket.java new file mode 100644 index 0000000..4dd9318 --- /dev/null +++ b/knoda/src/B050_Ticket.java @@ -0,0 +1,93 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B050_Ticket { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + final int ticketNum = scan.nextInt(); // ticketNum : チケットの数 + final String str = scan.next(); // str : 指定された文字列 + + // ticketStringList : すべてのチケットが格納されているリスト + final List ticketStringList = new ArrayList(); + // チケットの文字列を入力する + for (int i = 0; i < ticketNum; i++) { + ticketStringList.add(scan.next()); + } + + Ticket ticket = new Ticket(); + // validAndInvalidList : チケットが有効か無効かを格納するリスト + // チケットが有効かどうかを取得する + final List validAndInvalidList = ticket.getTicketValid(str, ticketStringList, ticketNum); + for (String result : validAndInvalidList) { + System.out.println(result); + } + scan.close(); + } + +} + +// チケットクラス +class Ticket { + // validAndInvalidList : チケットが有効か無効かを格納するリスト + final List validAndInvalidList = new ArrayList(); + + // チケットが有効かどうかを取得するメソッド + public List getTicketValid(final String str, final List ticketStringList, + final int ticketNum) { + // それぞれのチケットが有効かどうかを調べる + for (String ticketString : ticketStringList) { + boolean isValid = false; // isValid : チケットが有効かどうかを判定する(true : 有効、false :無効) + + // チケットの1文字目から調べる + for (int startIndex = 0; startIndex < ticketString.length() - str.length() + 1; startIndex++) { + int strIndex = 0; // 指定した文字列に含まれている文字の要素番号 + int ticketStringIndex = startIndex; // チケットの文字列に含まれている文字の要素番号 + int extraCharCount = 0; // チケットに存在する余計な文字の数 + + // チケットの文字列が1文字目から間違っている、もしくは余計な文字がすでに1文字含まれていたらループから抜ける + while (ticketStringIndex < ticketString.length()) { + if (ticketString.charAt(ticketStringIndex) == str.charAt(strIndex)) { + // 指定した文字列の文字とチケットの文字列の文字が同じとき + strIndex++; + } else if ((strIndex > 0) && (extraCharCount == 0)) { + // チケットの文字列に余計な文字が存在していないとき + extraCharCount++; + } else if (strIndex > 0) { + // チケットの文字列に余計な文字がすでに1文字存在しているとき + extraCharCount = 0; + strIndex = 0; + break; + } else { + // チケットの文字列が1文字目から間違っているとき + break; + } + + // 有効なチケットのとき、validを格納する + if (str.length() == strIndex) { + isValid = true; + validAndInvalidList.add("valid"); + break; + } + ticketStringIndex++; + } + + // 有効なチケットのとき + if (isValid) { + break; + } + } + + // 有効なチケットでないとき、invalidを格納する + if (!isValid) { + validAndInvalidList.add("invalid"); + } + } + return validAndInvalidList; + } + +} \ No newline at end of file diff --git a/knoda/src/B089_SecretWord.java b/knoda/src/B089_SecretWord.java deleted file mode 100644 index e1741ef..0000000 --- a/knoda/src/B089_SecretWord.java +++ /dev/null @@ -1,107 +0,0 @@ -package hellojunit; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class B089_SecretWord { - - public static void main(String[] args) { - final int INDEX_ZERO = 0; // 0番目のインデックス - final int INDEX_ONE = 1; // 1番目のインデックス - - Scanner scan = new Scanner(System.in); - - final int boardSize = scan.nextInt(); // boardSize : 基盤のサイズ - final int wordNum = scan.nextInt(); // wordNum : 単語の数 - final List characterList = new ArrayList(); // characterList : 基盤に記載されている文字 - - // 基盤に記載されている文字を入力する - for (int i = 0; i < boardSize; i++) { - characterList.add(scan.next()); - } - - // 単語を入力する - final List wordList = new ArrayList(); - for (int i = 0; i < wordNum; i++) { - wordList.add(scan.next()); - } - - - SecretWord secretWord = new SecretWord(); - - // 単語の先頭文字の位置を取得する - final List> rowAndColumnList = - secretWord.getColumnAndRowInWord(characterList, wordList, boardSize); - // 単語の先頭文字の位置を出力する - for (List rowAndColumn : rowAndColumnList) { - System.out.print((rowAndColumn.get(INDEX_ZERO) + 1) + " "); - System.out.println(rowAndColumn.get(INDEX_ONE) + 1); - } - scan.close(); - } - -} - -// 単語の先頭文字の位置を取得するクラス -class SecretWord { - - // それぞれの単語の先頭文字の位置を取得するメソッド - public List> getColumnAndRowInWord(final List characterList, final List wordList, - final int boardSize) { - // columnAndRowList : それぞれの単語の先頭文字の位置を格納するリスト - final List> columnAndRowList = new ArrayList>(); - - // それぞれの単語の先頭文字の位置を取得する - for (String word : wordList) { - // columnAndRow : ある単語の先頭文字の位置を格納するリスト - // ある単語の先頭文字の位置を取得する - List columnAndRow = getColumnAndRow(characterList, word, boardSize); - columnAndRowList.add(columnAndRow); - } - return columnAndRowList; - } - - // ある単語の先頭文字の位置を取得するメソッド - private List getColumnAndRow(final List characterList, final String word, - final int boardSize) { - - // columnAndRow : ある単語の先頭文字の位置を格納するリスト - final List columnAndRow = new ArrayList(); - - boolean isFindWord = false; // isFindWord : 単語が見つかったかの判定(true : 見つかった、false : 見つかっていない) - - // 基盤に書かれている文字を1文字ずつ走査する - for (int row = 0; row < boardSize - word.length() + 1; row++) { - for (int column = 0; column < boardSize - word.length() + 1; column++) { - int tmpRow = row; - int tmpColumn = column; - int wordIndex = 0; // wordIndex : 単語の文字の要素番号 - - // 単語が基盤に存在しているか調べる - while (wordIndex < word.length()) { - if (characterList.get(tmpRow).charAt(tmpColumn) == word.charAt(wordIndex)) { - tmpRow++; - tmpColumn++; - wordIndex++; - } else { - break; - } - } - - // 単語が基盤に存在しているとき - if (wordIndex == word.length()) { - columnAndRow.add(column); - columnAndRow.add(row); - isFindWord = true; - } - } - - // 単語が見つかったら終了 - if (isFindWord) { - break; - } - } - return columnAndRow; - } -} diff --git a/knoda/test/B050_TicketTest.java b/knoda/test/B050_TicketTest.java new file mode 100644 index 0000000..1f37acf --- /dev/null +++ b/knoda/test/B050_TicketTest.java @@ -0,0 +1,125 @@ +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 B050_TicketTest { + int ticketNum; + String str; + List ticketStringList; + Ticket sut; + + @Test + public void テスト1() { + ticketNum = 7; + str = "paiza"; + ticketStringList = new ArrayList(); + + ticketStringList.add("sdfpaizaoiu"); + ticketStringList.add("sdfpaizoiu"); + ticketStringList.add("paxiza"); + ticketStringList.add("paxizya"); + ticketStringList.add("ghepaizmakbn"); + ticketStringList.add("paizzza"); + ticketStringList.add("abcpadizzzaopq"); + + sut = new Ticket(); + List actualList = sut.getTicketValid(str, ticketStringList, ticketNum); + List expectedList = new ArrayList(); + + expectedList.add("valid"); + expectedList.add("invalid"); + expectedList.add("valid"); + expectedList.add("invalid"); + expectedList.add("valid"); + expectedList.add("invalid"); + expectedList.add("invalid"); + + for (int i = 0; i < actualList.size(); i++) { + assertThat(actualList.get(i), is(expectedList.get(i))); + } + } + + @Test + public void テスト2() { + ticketNum = 11; + str = "gbyw"; + ticketStringList = new ArrayList(); + + ticketStringList.add("gbyw"); + ticketStringList.add("ggbyw"); + ticketStringList.add("gbyws"); + ticketStringList.add("b"); + ticketStringList.add("kwflzgltejlcaosivfsy"); + ticketStringList.add("gbyl"); + ticketStringList.add("ogbyw"); + ticketStringList.add("ejyt"); + ticketStringList.add("yjlih"); + ticketStringList.add("bzbrquhbujsxnnoxfsw"); + ticketStringList.add("ygowvgibywkw"); + + sut = new Ticket(); + List actualList = sut.getTicketValid(str, ticketStringList, ticketNum); + List expectedList = new ArrayList(); + + expectedList.add("valid"); + expectedList.add("valid"); + expectedList.add("valid"); + expectedList.add("invalid"); + expectedList.add("invalid"); + expectedList.add("invalid"); + expectedList.add("valid"); + expectedList.add("invalid"); + expectedList.add("invalid"); + expectedList.add("invalid"); + expectedList.add("valid"); + + for (int i = 0; i < actualList.size(); i++) { + assertThat(actualList.get(i), is(expectedList.get(i))); + } + } + + @Test + public void 指定された文字列がaaaaaのときvalidが出力されるテスト() { + ticketNum = 1; + str = "aaaaa"; + ticketStringList = new ArrayList(); + + ticketStringList.add("aaabbabcabaaaa"); + + sut = new Ticket(); + List actualList = sut.getTicketValid(str, ticketStringList, ticketNum); + List expectedList = new ArrayList(); + + expectedList.add("valid"); + + for (int i = 0; i < actualList.size(); i++) { + assertThat(actualList.get(i), is(expectedList.get(i))); + } + } + + @Test + public void 指定された文字列がaaaaaのときinvalidが出力されるテスト() { + ticketNum = 1; + str = "aaaaa"; + ticketStringList = new ArrayList(); + + ticketStringList.add("aaabbaacbaaababaa"); + + sut = new Ticket(); + List actualList = sut.getTicketValid(str, ticketStringList, ticketNum); + List expectedList = new ArrayList(); + + expectedList.add("invalid"); + + for (int i = 0; i < actualList.size(); i++) { + assertThat(actualList.get(i), is(expectedList.get(i))); + } + } + +} diff --git a/knoda/test/B089_SecretWordTest.java b/knoda/test/B089_SecretWordTest.java deleted file mode 100644 index db2d734..0000000 --- a/knoda/test/B089_SecretWordTest.java +++ /dev/null @@ -1,98 +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 B089_SecretWordTest { - List characterList; - List wordList; - int boardSize; - SecretWord sut; - - @Test - public void 基盤に単語が存在しているテスト1() { - characterList = new ArrayList(); - wordList = new ArrayList(); - boardSize = 6; - - characterList.add("HPPLLM"); - characterList.add("UROQUV"); - characterList.add("FBSRZY"); - characterList.add("DPEFKT"); - characterList.add("GBBEUY"); - characterList.add("EMCQFY"); - - wordList.add("BEEF"); - wordList.add("PORK"); - - sut = new SecretWord(); - List> expectedList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(2, 3)), - new ArrayList(Arrays.asList(2, 1)))); - List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); - for (int i = 0; i < expectedList.size(); i++) { - for (int j = 0; j < expectedList.get(i).size(); j++) { - assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); - } - } - } - - @Test - public void 基盤に単語が存在しているテスト2() { - characterList = new ArrayList(); - wordList = new ArrayList(); - boardSize = 4; - - characterList.add("ACEG"); - characterList.add("HBDF"); - characterList.add("EGAC"); - characterList.add("DFHB"); - - wordList.add("ABA"); - wordList.add("BAB"); - - sut = new SecretWord(); - List> expectedList = new ArrayList>(Arrays.asList( - new ArrayList(Arrays.asList(1, 1)), - new ArrayList(Arrays.asList(2, 2)))); - List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); - for (int i = 0; i < expectedList.size(); i++) { - for (int j = 0; j < expectedList.get(i).size(); j++) { - assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); - } - } - } - - @Test - public void 基盤に単語が存在していないテスト() { - characterList = new ArrayList(); - wordList = new ArrayList(); - boardSize = 4; - - characterList.add("ACEG"); - characterList.add("HBDF"); - characterList.add("EGAC"); - characterList.add("DFHB"); - - wordList.add("HAF"); - wordList.add("BAB"); - - sut = new SecretWord(); - List> expectedList = new ArrayList>(Arrays.asList( - new ArrayList(), - new ArrayList(Arrays.asList(2, 2)))); - List> actualList = sut.getColumnAndRowInWord(characterList, wordList, boardSize); - for (int i = 0; i < expectedList.size(); i++) { - for (int j = 0; j < expectedList.get(i).size(); j++) { - assertThat(actualList.get(i).get(j) + 1, is(expectedList.get(i).get(j))); - } - } - } - -} -- GitLab From cb6a5915511e6d31c1d32d87600e535340ad5668 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, 29 Jul 2022 17:18:16 +0900 Subject: [PATCH 16/18] =?UTF-8?q?B050=20=E6=9C=89=E5=8A=B9=E3=81=AA?= =?UTF-8?q?=E3=83=81=E3=82=B1=E3=83=83=E3=83=88=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/B050_Ticket.java | 90 ++++++++++++++++++--------------- knoda/test/B050_TicketTest.java | 44 +++++++--------- 2 files changed, 67 insertions(+), 67 deletions(-) diff --git a/knoda/src/B050_Ticket.java b/knoda/src/B050_Ticket.java index 4dd9318..569735f 100644 --- a/knoda/src/B050_Ticket.java +++ b/knoda/src/B050_Ticket.java @@ -7,10 +7,10 @@ import java.util.Scanner; public class B050_Ticket { public static void main(String[] args) { - Scanner scan = new Scanner(System.in); + final Scanner scan = new Scanner(System.in); final int ticketNum = scan.nextInt(); // ticketNum : チケットの数 - final String str = scan.next(); // str : 指定された文字列 + final String keyword = scan.next(); // str : 指定された文字列 // ticketStringList : すべてのチケットが格納されているリスト final List ticketStringList = new ArrayList(); @@ -19,11 +19,11 @@ public class B050_Ticket { ticketStringList.add(scan.next()); } - Ticket ticket = new Ticket(); + final TicketList ticketList = new TicketList(); // validAndInvalidList : チケットが有効か無効かを格納するリスト // チケットが有効かどうかを取得する - final List validAndInvalidList = ticket.getTicketValid(str, ticketStringList, ticketNum); - for (String result : validAndInvalidList) { + final List validAndInvalidList = ticketList.getIsTicketValid(keyword, ticketStringList); + for (final String result : validAndInvalidList) { System.out.println(result); } scan.close(); @@ -32,49 +32,28 @@ public class B050_Ticket { } // チケットクラス -class Ticket { +class TicketList { // validAndInvalidList : チケットが有効か無効かを格納するリスト - final List validAndInvalidList = new ArrayList(); + private final List validAndInvalidList = new ArrayList(); + private int keywordIndex; // 指定した文字列に含まれている文字の要素番号 + private int ticketStringIndex; // チケットの文字列に含まれている文字の要素番号 + private int extraCharCount; // チケットに存在する余計な文字の数 + private boolean isValid; // チケットが有効かを判定 // チケットが有効かどうかを取得するメソッド - public List getTicketValid(final String str, final List ticketStringList, - final int ticketNum) { + public List getIsTicketValid(final String keyword, final List ticketStringList) { // それぞれのチケットが有効かどうかを調べる - for (String ticketString : ticketStringList) { - boolean isValid = false; // isValid : チケットが有効かどうかを判定する(true : 有効、false :無効) + for (final String ticketString : ticketStringList) { + isValid = false; // チケットの1文字目から調べる - for (int startIndex = 0; startIndex < ticketString.length() - str.length() + 1; startIndex++) { - int strIndex = 0; // 指定した文字列に含まれている文字の要素番号 - int ticketStringIndex = startIndex; // チケットの文字列に含まれている文字の要素番号 - int extraCharCount = 0; // チケットに存在する余計な文字の数 + for (int startIndex = 0; startIndex < ticketString.length() - keyword.length() + 1; startIndex++) { + keywordIndex = 0; + ticketStringIndex = startIndex; + extraCharCount = 0; - // チケットの文字列が1文字目から間違っている、もしくは余計な文字がすでに1文字含まれていたらループから抜ける - while (ticketStringIndex < ticketString.length()) { - if (ticketString.charAt(ticketStringIndex) == str.charAt(strIndex)) { - // 指定した文字列の文字とチケットの文字列の文字が同じとき - strIndex++; - } else if ((strIndex > 0) && (extraCharCount == 0)) { - // チケットの文字列に余計な文字が存在していないとき - extraCharCount++; - } else if (strIndex > 0) { - // チケットの文字列に余計な文字がすでに1文字存在しているとき - extraCharCount = 0; - strIndex = 0; - break; - } else { - // チケットの文字列が1文字目から間違っているとき - break; - } - - // 有効なチケットのとき、validを格納する - if (str.length() == strIndex) { - isValid = true; - validAndInvalidList.add("valid"); - break; - } - ticketStringIndex++; - } + // チケットの文字列を走査する + characterTraversal(keywordIndex, ticketStringIndex, extraCharCount, ticketString, keyword); // 有効なチケットのとき if (isValid) { @@ -90,4 +69,33 @@ class Ticket { return validAndInvalidList; } + // チケットの文字列を走査するメソッド + private void characterTraversal(int keywordIndex, int ticketStringIndex, int extraCharCount, + final String ticketString, final String keyword) { + // チケットの文字列が1文字目から間違っている、もしくは余計な文字がすでに1文字含まれていたらループから抜ける + while (ticketStringIndex < ticketString.length()) { + if (ticketString.charAt(ticketStringIndex) == keyword.charAt(keywordIndex)) { + // 指定した文字列の文字とチケットの文字列の文字が同じとき + keywordIndex++; + // 有効なチケットのとき、validを格納する + if (keyword.length() == keywordIndex) { + isValid = true; + validAndInvalidList.add("valid"); + break; + } + + } else if ((keywordIndex > 0) && (extraCharCount == 0)) { + // チケットの文字列に余計な文字が存在していないとき + extraCharCount++; + } else if (keywordIndex > 0) { + // チケットの文字列に余計な文字がすでに1文字存在しているとき + break; + } else { + // チケットの文字列が1文字目から間違っているとき + break; + } + ticketStringIndex++; + } + } + } \ No newline at end of file diff --git a/knoda/test/B050_TicketTest.java b/knoda/test/B050_TicketTest.java index 1f37acf..80ec2b1 100644 --- a/knoda/test/B050_TicketTest.java +++ b/knoda/test/B050_TicketTest.java @@ -9,16 +9,11 @@ import java.util.List; import org.junit.Test; public class B050_TicketTest { - int ticketNum; - String str; - List ticketStringList; - Ticket sut; @Test public void テスト1() { - ticketNum = 7; - str = "paiza"; - ticketStringList = new ArrayList(); + String keyword = "paiza"; + List ticketStringList = new ArrayList(); ticketStringList.add("sdfpaizaoiu"); ticketStringList.add("sdfpaizoiu"); @@ -28,8 +23,8 @@ public class B050_TicketTest { ticketStringList.add("paizzza"); ticketStringList.add("abcpadizzzaopq"); - sut = new Ticket(); - List actualList = sut.getTicketValid(str, ticketStringList, ticketNum); + TicketList ticketList = new TicketList(); + List actualList = ticketList.getIsTicketValid(keyword, ticketStringList); List expectedList = new ArrayList(); expectedList.add("valid"); @@ -47,9 +42,8 @@ public class B050_TicketTest { @Test public void テスト2() { - ticketNum = 11; - str = "gbyw"; - ticketStringList = new ArrayList(); + String keyword = "gbyw"; + List ticketStringList = new ArrayList(); ticketStringList.add("gbyw"); ticketStringList.add("ggbyw"); @@ -63,8 +57,8 @@ public class B050_TicketTest { ticketStringList.add("bzbrquhbujsxnnoxfsw"); ticketStringList.add("ygowvgibywkw"); - sut = new Ticket(); - List actualList = sut.getTicketValid(str, ticketStringList, ticketNum); + TicketList ticketList = new TicketList(); + List actualList = ticketList.getIsTicketValid(keyword, ticketStringList); List expectedList = new ArrayList(); expectedList.add("valid"); @@ -85,15 +79,14 @@ public class B050_TicketTest { } @Test - public void 指定された文字列がaaaaaのときvalidが出力されるテスト() { - ticketNum = 1; - str = "aaaaa"; - ticketStringList = new ArrayList(); + public void 指定された文字列に1文字余計な文字が入った場合はvalid() { + String keyword = "aaaaa"; + List ticketStringList = new ArrayList(); ticketStringList.add("aaabbabcabaaaa"); - sut = new Ticket(); - List actualList = sut.getTicketValid(str, ticketStringList, ticketNum); + TicketList ticketList = new TicketList(); + List actualList = ticketList.getIsTicketValid(keyword, ticketStringList); List expectedList = new ArrayList(); expectedList.add("valid"); @@ -104,15 +97,14 @@ public class B050_TicketTest { } @Test - public void 指定された文字列がaaaaaのときinvalidが出力されるテスト() { - ticketNum = 1; - str = "aaaaa"; - ticketStringList = new ArrayList(); + public void 指定された文字列に2文字余計な文字が入った場合はinvalid() { + String keyword = "aaaaa"; + List ticketStringList = new ArrayList(); ticketStringList.add("aaabbaacbaaababaa"); - sut = new Ticket(); - List actualList = sut.getTicketValid(str, ticketStringList, ticketNum); + TicketList ticketList = new TicketList(); + List actualList = ticketList.getIsTicketValid(keyword, ticketStringList); List expectedList = new ArrayList(); expectedList.add("invalid"); -- GitLab From f3375a9ec4dd1d6e85ab0fa80af901a983354cde 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, 1 Aug 2022 09:03:49 +0900 Subject: [PATCH 17/18] =?UTF-8?q?B050=20=E6=9C=89=E5=8A=B9=E3=81=AA?= =?UTF-8?q?=E3=83=81=E3=82=B1=E3=83=83=E3=83=88=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?Ver1.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B050_Ticket.java | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/knoda/src/B050_Ticket.java b/knoda/src/B050_Ticket.java index 569735f..2032065 100644 --- a/knoda/src/B050_Ticket.java +++ b/knoda/src/B050_Ticket.java @@ -33,27 +33,24 @@ public class B050_Ticket { // チケットクラス class TicketList { - // validAndInvalidList : チケットが有効か無効かを格納するリスト - private final List validAndInvalidList = new ArrayList(); - private int keywordIndex; // 指定した文字列に含まれている文字の要素番号 - private int ticketStringIndex; // チケットの文字列に含まれている文字の要素番号 - private int extraCharCount; // チケットに存在する余計な文字の数 private boolean isValid; // チケットが有効かを判定 // チケットが有効かどうかを取得するメソッド public List getIsTicketValid(final String keyword, final List ticketStringList) { + // validAndInvalidList : チケットが有効か無効かを格納するリスト + final List validAndInvalidList = new ArrayList(); // それぞれのチケットが有効かどうかを調べる for (final String ticketString : ticketStringList) { isValid = false; // チケットの1文字目から調べる for (int startIndex = 0; startIndex < ticketString.length() - keyword.length() + 1; startIndex++) { - keywordIndex = 0; - ticketStringIndex = startIndex; - extraCharCount = 0; + final int keywordIndex = 0; + final int ticketStringIndex = startIndex; + final int extraCharCount = 0; // チケットの文字列を走査する - characterTraversal(keywordIndex, ticketStringIndex, extraCharCount, ticketString, keyword); + isValid = characterTraversal(keywordIndex, ticketStringIndex, extraCharCount, ticketString, keyword); // 有効なチケットのとき if (isValid) { @@ -61,8 +58,10 @@ class TicketList { } } - // 有効なチケットでないとき、invalidを格納する - if (!isValid) { + // チケットが有効か無効かを格納する + if (isValid) { + validAndInvalidList.add("valid"); + } else { validAndInvalidList.add("invalid"); } } @@ -70,7 +69,7 @@ class TicketList { } // チケットの文字列を走査するメソッド - private void characterTraversal(int keywordIndex, int ticketStringIndex, int extraCharCount, + private boolean characterTraversal(int keywordIndex, int ticketStringIndex, int extraCharCount, final String ticketString, final String keyword) { // チケットの文字列が1文字目から間違っている、もしくは余計な文字がすでに1文字含まれていたらループから抜ける while (ticketStringIndex < ticketString.length()) { @@ -80,7 +79,6 @@ class TicketList { // 有効なチケットのとき、validを格納する if (keyword.length() == keywordIndex) { isValid = true; - validAndInvalidList.add("valid"); break; } @@ -89,13 +87,16 @@ class TicketList { extraCharCount++; } else if (keywordIndex > 0) { // チケットの文字列に余計な文字がすでに1文字存在しているとき + isValid = false; break; } else { // チケットの文字列が1文字目から間違っているとき + isValid = false; break; } ticketStringIndex++; } + return isValid; } } \ No newline at end of file -- GitLab From 948541b27506f26c8c5493de55d0ac468b250f9c 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, 1 Aug 2022 09:41:04 +0900 Subject: [PATCH 18/18] =?UTF-8?q?B050=20=E6=9C=89=E5=8A=B9=E3=81=AA?= =?UTF-8?q?=E3=83=81=E3=82=B1=E3=83=83=E3=83=88=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?ver1.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knoda/src/B050_Ticket.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/knoda/src/B050_Ticket.java b/knoda/src/B050_Ticket.java index 2032065..a16c6f2 100644 --- a/knoda/src/B050_Ticket.java +++ b/knoda/src/B050_Ticket.java @@ -9,8 +9,8 @@ public class B050_Ticket { public static void main(String[] args) { final Scanner scan = new Scanner(System.in); - final int ticketNum = scan.nextInt(); // ticketNum : チケットの数 - final String keyword = scan.next(); // str : 指定された文字列 + final int ticketNum = scan.nextInt(); + final String keyword = scan.next(); // ticketStringList : すべてのチケットが格納されているリスト final List ticketStringList = new ArrayList(); @@ -33,7 +33,6 @@ public class B050_Ticket { // チケットクラス class TicketList { - private boolean isValid; // チケットが有効かを判定 // チケットが有効かどうかを取得するメソッド public List getIsTicketValid(final String keyword, final List ticketStringList) { @@ -41,7 +40,7 @@ class TicketList { final List validAndInvalidList = new ArrayList(); // それぞれのチケットが有効かどうかを調べる for (final String ticketString : ticketStringList) { - isValid = false; + boolean isValid = false; // チケットが有効かを判定 // チケットの1文字目から調べる for (int startIndex = 0; startIndex < ticketString.length() - keyword.length() + 1; startIndex++) { @@ -78,8 +77,7 @@ class TicketList { keywordIndex++; // 有効なチケットのとき、validを格納する if (keyword.length() == keywordIndex) { - isValid = true; - break; + return true; } } else if ((keywordIndex > 0) && (extraCharCount == 0)) { @@ -87,16 +85,14 @@ class TicketList { extraCharCount++; } else if (keywordIndex > 0) { // チケットの文字列に余計な文字がすでに1文字存在しているとき - isValid = false; break; } else { // チケットの文字列が1文字目から間違っているとき - isValid = false; break; } ticketStringIndex++; } - return isValid; + return false; } } \ No newline at end of file -- GitLab