From bed6d2f4f0db3217f14186d12b85032e337e3649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Fri, 2 Aug 2024 17:28:38 +0900 Subject: [PATCH 01/12] =?UTF-8?q?paiza=E3=81=AEA083=E3=81=AE=E5=9B=9E?= =?UTF-8?q?=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/A083_SecretMessage.java | 154 ++++++++++++++++++++++++++++++ sitou/src/Adventurer.java | 15 +++ sitou/src/InputData.java | 16 ++++ sitou/src/Parallel.java | 21 ++++ sitou/src/Result.java | 13 +++ sitou/src/TreasureMap.java | 26 +++++ sitou/test/A083_Test.java | 37 +++++++ sitou/test/TreasureMapTest.java | 73 ++++++++++++++ 8 files changed, 355 insertions(+) create mode 100644 sitou/src/A083_SecretMessage.java create mode 100644 sitou/src/Adventurer.java create mode 100644 sitou/src/InputData.java create mode 100644 sitou/src/Parallel.java create mode 100644 sitou/src/Result.java create mode 100644 sitou/src/TreasureMap.java create mode 100644 sitou/test/A083_Test.java create mode 100644 sitou/test/TreasureMapTest.java diff --git a/sitou/src/A083_SecretMessage.java b/sitou/src/A083_SecretMessage.java new file mode 100644 index 0000000..5eb1009 --- /dev/null +++ b/sitou/src/A083_SecretMessage.java @@ -0,0 +1,154 @@ +package src; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Queue; +import java.util.Scanner; + +public class A083_SecretMessage { + + public static void main(String[] args) { + execute(); + } + + static void execute() { + final InputData inputData = input(); + final TreasureMap treasureMap = new TreasureMap(inputData); + final Result result = solve(treasureMap); + output(result); + } + + private static InputData input() { + final Scanner scan = new Scanner(System.in); + final int height = scan.nextInt(); + final int width = scan.nextInt(); + final List mapStatus = new ArrayList(); + + for (int i = 0; i < height; i++) { + mapStatus.add(scan.next()); + } + + scan.close(); + return new InputData(height, width, mapStatus); + + } + + public static Result solve(TreasureMap treasureMap) { + + String result = treasureMap.world.get(0).get(0); + final List resultList = new ArrayList(); + + // 初期値格納 + final Parallel first_time = new Parallel(0, 0, treasureMap.world, result); + final Queue queue = new ArrayDeque<>(); + queue.add(first_time); + + while (true) { + // キューからデータを受け取る + final Parallel parallel = queue.poll(); + + final int nowX = parallel.point.x; + final int nowY = parallel.point.y; + result = parallel.p_result; + + // 世界はディープコピーする + List> now_world = new ArrayList>(); + for (int i = 0; i < treasureMap.height; i++) { + List list = (List) ((ArrayList) parallel.world.get(i)).clone(); + now_world.add(list); + } + + // 今いる場所を次に来れないようにする + now_world.get(nowY).set(nowX, "zzz"); + Adventurer ad = new Adventurer(nowX, nowY); + + // 4方向の文字を確認していく + String sinmoji = "zz"; + int nextX = nowX; + int nextY = nowY; + if ((ad.point.x) + 1 < treasureMap.width && sinmoji + .compareTo(now_world.get(ad.point.y).get((ad.point.x) + 1)) >= 0) { + if (sinmoji.compareTo(now_world.get(ad.point.y).get((ad.point.x) + 1)) == 0) { + final String p_result = result + sinmoji; + final Parallel newParallel = new Parallel((ad.point.x) + 1, ad.point.y, now_world, + p_result); + + queue.add(newParallel); + + } else { + sinmoji = now_world.get(ad.point.y).get(ad.point.x + 1); + nextX = (ad.point.x) + 1; + nextY = ad.point.y; + } + } + if ((ad.point.y) + 1 < treasureMap.height && sinmoji + .compareTo(now_world.get((ad.point.y) + 1).get(ad.point.x)) >= 0) { + if (sinmoji.compareTo(now_world.get((ad.point.y) + 1).get(ad.point.x)) == 0) { + final String p_result = result + sinmoji; + final Parallel newParallel = new Parallel(ad.point.x, (ad.point.y) + 1, now_world, + p_result); + queue.add(newParallel); + + } else { + sinmoji = now_world.get((ad.point.y) + 1).get(ad.point.x); + nextX = ad.point.x; + nextY = (ad.point.y) + 1; + } + } + if (ad.point.x > 0 && sinmoji + .compareTo(now_world.get(ad.point.y).get((ad.point.x) - 1)) >= 0) { + if (sinmoji.compareTo(now_world.get(ad.point.y).get((ad.point.x) - 1)) == 0) { + final String p_result = result + sinmoji; + final Parallel newParallel = new Parallel((ad.point.x) - 1, ad.point.y, now_world, + p_result); + + queue.add(newParallel); + + } else { + sinmoji = now_world.get(ad.point.y).get((ad.point.x) - 1); + nextX = (ad.point.x) - 1; + nextY = ad.point.y; + } + } + if (ad.point.y > 0 && sinmoji + .compareTo(now_world.get((ad.point.y) - 1).get(ad.point.x)) >= 0) { + if (sinmoji.compareTo(now_world.get((ad.point.y) - 1).get(ad.point.x)) == 0) { + final String p_result = result + sinmoji; + final Parallel newParallel = new Parallel(ad.point.x, (ad.point.y) - 1, now_world, + p_result); + + queue.add(newParallel); + + } else { + sinmoji = now_world.get((ad.point.y) - 1).get(ad.point.x); + nextX = ad.point.x; + nextY = (ad.point.y) - 1; + } + } + + final String p_result = result + sinmoji; + + if (nextX == (treasureMap.width) - 1 && nextY == (treasureMap.height) - 1) { + resultList.add(p_result); + } else { + final Parallel newParallel = new Parallel(nextX, nextY, now_world, p_result); + queue.add(newParallel); + } + + if (queue.isEmpty()) { + break; + } + } + + Collections.sort(resultList); + final String resultMassage = resultList.get(0); + return new Result(resultMassage); + } + + private static void output(final Result result) { + System.out.println(result.getMessage()); + } + +} diff --git a/sitou/src/Adventurer.java b/sitou/src/Adventurer.java new file mode 100644 index 0000000..633cb80 --- /dev/null +++ b/sitou/src/Adventurer.java @@ -0,0 +1,15 @@ +package src; + +import java.awt.Point; + +public class Adventurer { + + //冒険家の座標クラス + + final Point point = new Point(); + + public Adventurer(final int x, final int y) { + point.setLocation(x, y); + } + +} diff --git a/sitou/src/InputData.java b/sitou/src/InputData.java new file mode 100644 index 0000000..6aa64f6 --- /dev/null +++ b/sitou/src/InputData.java @@ -0,0 +1,16 @@ +package src; + +import java.util.List; + +public class InputData { + final int height; + final int width; + final List mapStatus; + + public InputData(final int height, final int width, final List mapStatus) { + this.height = height; + this.width = width; + this.mapStatus = mapStatus; + } + +} diff --git a/sitou/src/Parallel.java b/sitou/src/Parallel.java new file mode 100644 index 0000000..099210c --- /dev/null +++ b/sitou/src/Parallel.java @@ -0,0 +1,21 @@ +package src; + +import java.awt.Point; +import java.util.ArrayList; +import java.util.List; + +public class Parallel { + + //queue用にデータをまとめるクラス + + final Point point = new Point(); + List> world = new ArrayList>(); + final String p_result; + + public Parallel(int x, int y, List> world, String p_result) { + point.x = x; + point.y = y; + this.world = world; + this.p_result = p_result; + } +} diff --git a/sitou/src/Result.java b/sitou/src/Result.java new file mode 100644 index 0000000..dc4d4d3 --- /dev/null +++ b/sitou/src/Result.java @@ -0,0 +1,13 @@ +package src; + +public class Result { + final String message; + + public Result(final String message) { + this.message = message; + } + + public final String getMessage() { + return message; + } +} diff --git a/sitou/src/TreasureMap.java b/sitou/src/TreasureMap.java new file mode 100644 index 0000000..9d39c13 --- /dev/null +++ b/sitou/src/TreasureMap.java @@ -0,0 +1,26 @@ +package src; + +import java.util.ArrayList; +import java.util.List; + +public class TreasureMap { + final int height; + final int width; + final List> world = new ArrayList>(); + + public TreasureMap(InputData inputData) { + height = inputData.height; + width = inputData.width; + + //scanした文字列を分解して1文字1マスで扱えるようにする + for (int i = 0; i < height; i++) { + final List list = new ArrayList<>(); + final String mapChar = inputData.mapStatus.get(i); + for (int j = 0; j < width; j++) { + list.add(mapChar.substring(j, j + 1)); + } + world.add(list); + } + } + +} diff --git a/sitou/test/A083_Test.java b/sitou/test/A083_Test.java new file mode 100644 index 0000000..5fc6371 --- /dev/null +++ b/sitou/test/A083_Test.java @@ -0,0 +1,37 @@ +package test; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import java.util.Arrays; +import org.junit.Test; +import src.A083_SecretMessage; +import src.InputData; +import src.Result; +import src.TreasureMap; + +public class A083_Test { + @Test + public void 正しく探索できるかテスト1() { + + final InputData inputData = new InputData(3,5,Arrays.asList("hello","paiza","world")); + TreasureMap treasureMap = new TreasureMap(inputData); + final Result result = A083_SecretMessage.solve(treasureMap); + String actual = result.getMessage(); + + final String expected = "heailload"; + + assertThat(actual, is(expected)); + } + + public void 最も辞書順で小さいメッセージを探索できるかテスト() { + + final InputData inputData = new InputData(3,3,Arrays.asList("abc","bcx","cyz")); + TreasureMap treasureMap = new TreasureMap(inputData); + final Result result = A083_SecretMessage.solve(treasureMap); + String actual = result.getMessage(); + + final String expected = "abcbcxz"; + + assertThat(actual, is(expected)); + } +} diff --git a/sitou/test/TreasureMapTest.java b/sitou/test/TreasureMapTest.java new file mode 100644 index 0000000..6c52480 --- /dev/null +++ b/sitou/test/TreasureMapTest.java @@ -0,0 +1,73 @@ +package test; + +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 TreasureMapTest { + @Test + public void マスごとに正しく分割できるかテスト() { + + final List> actual = new ArrayList>(); + final List mapStatus = Arrays.asList("hello", "paiza", "world"); + for (int i = 0; i < 3; i++) { + final List list = new ArrayList<>(); + final String mapChar = mapStatus.get(i); + for (int j = 0; j < 5; j++) { + list.add(mapChar.substring(j, j + 1)); + } + actual.add(list); + } + + final List> expected = new ArrayList>(Arrays + .asList(Arrays.asList("h", "e", "l", "l", "o"), Arrays.asList("p", "a", "i", "z", "a"), + Arrays.asList("w", "o", "r", "l", "d"))); + + assertThat(actual, is(expected)); + + } + + @Test + public void 秘境の大きさが最小の時に正しく分割できるかテスト() { + final List> actual = new ArrayList>(); + final List mapStatus = Arrays.asList("a"); + for (int i = 0; i < 1; i++) { + final List list = new ArrayList<>(); + final String mapChar = mapStatus.get(i); + for (int j = 0; j < 1; j++) { + list.add(mapChar.substring(j, j + 1)); + } + actual.add(list); + } + + final List> expected = new ArrayList>(Arrays + .asList(Arrays.asList("a"))); + + assertThat(actual, is(expected)); + } + + @Test + public void 秘境の大きさが最大の時に正しく分割できるかテスト() { + final List> actual = new ArrayList>(); + final List mapStatus = Arrays + .asList("abcde", "fghij", "klmno", "pqrst", "uvwxy", "zzzzz"); + for (int i = 0; i < 6; i++) { + final List list = new ArrayList<>(); + final String mapChar = mapStatus.get(i); + for (int j = 0; j < 5; j++) { + list.add(mapChar.substring(j, j + 1)); + } + actual.add(list); + } + + final List> expected = new ArrayList>(Arrays + .asList(Arrays.asList("a", "b", "c", "d", "e"), Arrays.asList("f", "g", "h", "i", "j"), + Arrays.asList("k", "l", "m", "n", "o"), Arrays.asList("p", "q", "r", "s", "t"), Arrays + .asList("u", "v", "w", "x", "y"), Arrays.asList("z", "z", "z", "z", "z"))); + + assertThat(actual, is(expected)); + } +} -- GitLab From 33afccb99f895bd66c781772b6269cf88c4d20f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Tue, 13 Aug 2024 13:27:01 +0900 Subject: [PATCH 02/12] =?UTF-8?q?paiza=E3=81=AEA083=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/A083_SecretMessage.java | 140 ++++++++---------------------- sitou/src/Adventurer.java | 15 ---- sitou/src/InputData.java | 6 +- sitou/src/Parallel.java | 21 ----- sitou/src/Result.java | 2 +- sitou/src/TreasureMap.java | 32 ++++--- sitou/test/A083_Test.java | 30 +++++-- sitou/test/TreasureMapTest.java | 96 ++++++++++---------- 8 files changed, 131 insertions(+), 211 deletions(-) delete mode 100644 sitou/src/Adventurer.java delete mode 100644 sitou/src/Parallel.java diff --git a/sitou/src/A083_SecretMessage.java b/sitou/src/A083_SecretMessage.java index 5eb1009..afe5e3a 100644 --- a/sitou/src/A083_SecretMessage.java +++ b/sitou/src/A083_SecretMessage.java @@ -1,10 +1,7 @@ package src; -import java.util.ArrayDeque; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.Queue; import java.util.Scanner; public class A083_SecretMessage { @@ -24,10 +21,10 @@ public class A083_SecretMessage { final Scanner scan = new Scanner(System.in); final int height = scan.nextInt(); final int width = scan.nextInt(); - final List mapStatus = new ArrayList(); + final String[] mapStatus = new String[height]; for (int i = 0; i < height; i++) { - mapStatus.add(scan.next()); + mapStatus[i] = scan.next(); } scan.close(); @@ -36,115 +33,46 @@ public class A083_SecretMessage { } public static Result solve(TreasureMap treasureMap) { + boolean[][] visited = new boolean[treasureMap.getHeight()][treasureMap.getWidth()]; + List allPaths = new ArrayList<>(); + findPaths(0, 0, "", visited, treasureMap, allPaths); + + //辞書順で早いものを探索 + String smallestPath = allPaths.get(0); + for (String p : allPaths) { + if (p.compareTo(smallestPath) < 0) { + smallestPath = p; + } + } + return new Result(smallestPath); - String result = treasureMap.world.get(0).get(0); - final List resultList = new ArrayList(); - - // 初期値格納 - final Parallel first_time = new Parallel(0, 0, treasureMap.world, result); - final Queue queue = new ArrayDeque<>(); - queue.add(first_time); - - while (true) { - // キューからデータを受け取る - final Parallel parallel = queue.poll(); - - final int nowX = parallel.point.x; - final int nowY = parallel.point.y; - result = parallel.p_result; + } - // 世界はディープコピーする - List> now_world = new ArrayList>(); - for (int i = 0; i < treasureMap.height; i++) { - List list = (List) ((ArrayList) parallel.world.get(i)).clone(); - now_world.add(list); - } + public static void findPaths(int x, int y, String path, boolean[][] visited, + TreasureMap treasureMap, List allPaths) { + //上下左右確認用 + int[] dx = {-1, 1, 0, 0}; + int[] dy = {0, 0, -1, 1}; - // 今いる場所を次に来れないようにする - now_world.get(nowY).set(nowX, "zzz"); - Adventurer ad = new Adventurer(nowX, nowY); - - // 4方向の文字を確認していく - String sinmoji = "zz"; - int nextX = nowX; - int nextY = nowY; - if ((ad.point.x) + 1 < treasureMap.width && sinmoji - .compareTo(now_world.get(ad.point.y).get((ad.point.x) + 1)) >= 0) { - if (sinmoji.compareTo(now_world.get(ad.point.y).get((ad.point.x) + 1)) == 0) { - final String p_result = result + sinmoji; - final Parallel newParallel = new Parallel((ad.point.x) + 1, ad.point.y, now_world, - p_result); - - queue.add(newParallel); - - } else { - sinmoji = now_world.get(ad.point.y).get(ad.point.x + 1); - nextX = (ad.point.x) + 1; - nextY = ad.point.y; - } - } - if ((ad.point.y) + 1 < treasureMap.height && sinmoji - .compareTo(now_world.get((ad.point.y) + 1).get(ad.point.x)) >= 0) { - if (sinmoji.compareTo(now_world.get((ad.point.y) + 1).get(ad.point.x)) == 0) { - final String p_result = result + sinmoji; - final Parallel newParallel = new Parallel(ad.point.x, (ad.point.y) + 1, now_world, - p_result); - queue.add(newParallel); - - } else { - sinmoji = now_world.get((ad.point.y) + 1).get(ad.point.x); - nextX = ad.point.x; - nextY = (ad.point.y) + 1; - } - } - if (ad.point.x > 0 && sinmoji - .compareTo(now_world.get(ad.point.y).get((ad.point.x) - 1)) >= 0) { - if (sinmoji.compareTo(now_world.get(ad.point.y).get((ad.point.x) - 1)) == 0) { - final String p_result = result + sinmoji; - final Parallel newParallel = new Parallel((ad.point.x) - 1, ad.point.y, now_world, - p_result); - - queue.add(newParallel); - - } else { - sinmoji = now_world.get(ad.point.y).get((ad.point.x) - 1); - nextX = (ad.point.x) - 1; - nextY = ad.point.y; - } - } - if (ad.point.y > 0 && sinmoji - .compareTo(now_world.get((ad.point.y) - 1).get(ad.point.x)) >= 0) { - if (sinmoji.compareTo(now_world.get((ad.point.y) - 1).get(ad.point.x)) == 0) { - final String p_result = result + sinmoji; - final Parallel newParallel = new Parallel(ad.point.x, (ad.point.y) - 1, now_world, - p_result); - - queue.add(newParallel); - - } else { - sinmoji = now_world.get((ad.point.y) - 1).get(ad.point.x); - nextX = ad.point.x; - nextY = (ad.point.y) - 1; - } - } + //Map外や二度目に通る所のとき + if (x < 0 || y < 0 || x >= treasureMap.getWorld().length || y >= treasureMap.getWorld()[0].length || visited[x][y]) { + return; + } - final String p_result = result + sinmoji; + //ゴールのとき + if (x == treasureMap.getWorld().length - 1 && y == treasureMap.getWorld()[0].length - 1) { + allPaths.add(path + treasureMap.getWorld()[x][y]); + return; + } - if (nextX == (treasureMap.width) - 1 && nextY == (treasureMap.height) - 1) { - resultList.add(p_result); - } else { - final Parallel newParallel = new Parallel(nextX, nextY, now_world, p_result); - queue.add(newParallel); - } + visited[x][y] = true; + path += treasureMap.getWorld()[x][y]; - if (queue.isEmpty()) { - break; - } + for (int i = 0; i < 4; i++) { + findPaths(x + dx[i], y + dy[i], path, visited, treasureMap, allPaths); } - Collections.sort(resultList); - final String resultMassage = resultList.get(0); - return new Result(resultMassage); + visited[x][y] = false; } private static void output(final Result result) { diff --git a/sitou/src/Adventurer.java b/sitou/src/Adventurer.java deleted file mode 100644 index 633cb80..0000000 --- a/sitou/src/Adventurer.java +++ /dev/null @@ -1,15 +0,0 @@ -package src; - -import java.awt.Point; - -public class Adventurer { - - //冒険家の座標クラス - - final Point point = new Point(); - - public Adventurer(final int x, final int y) { - point.setLocation(x, y); - } - -} diff --git a/sitou/src/InputData.java b/sitou/src/InputData.java index 6aa64f6..fd402e3 100644 --- a/sitou/src/InputData.java +++ b/sitou/src/InputData.java @@ -1,13 +1,11 @@ package src; -import java.util.List; - public class InputData { final int height; final int width; - final List mapStatus; + final String[] mapStatus; - public InputData(final int height, final int width, final List mapStatus) { + public InputData(final int height, final int width, final String[] mapStatus) { this.height = height; this.width = width; this.mapStatus = mapStatus; diff --git a/sitou/src/Parallel.java b/sitou/src/Parallel.java deleted file mode 100644 index 099210c..0000000 --- a/sitou/src/Parallel.java +++ /dev/null @@ -1,21 +0,0 @@ -package src; - -import java.awt.Point; -import java.util.ArrayList; -import java.util.List; - -public class Parallel { - - //queue用にデータをまとめるクラス - - final Point point = new Point(); - List> world = new ArrayList>(); - final String p_result; - - public Parallel(int x, int y, List> world, String p_result) { - point.x = x; - point.y = y; - this.world = world; - this.p_result = p_result; - } -} diff --git a/sitou/src/Result.java b/sitou/src/Result.java index dc4d4d3..8d026fd 100644 --- a/sitou/src/Result.java +++ b/sitou/src/Result.java @@ -3,7 +3,7 @@ package src; public class Result { final String message; - public Result(final String message) { + Result(final String message) { this.message = message; } diff --git a/sitou/src/TreasureMap.java b/sitou/src/TreasureMap.java index 9d39c13..29f9b7c 100644 --- a/sitou/src/TreasureMap.java +++ b/sitou/src/TreasureMap.java @@ -1,26 +1,34 @@ package src; -import java.util.ArrayList; -import java.util.List; - public class TreasureMap { final int height; - final int width; - final List> world = new ArrayList>(); + private final int width; + final char[][] world; public TreasureMap(InputData inputData) { height = inputData.height; width = inputData.width; + world = new char[getHeight()][getWidth()]; + //scanした文字列を分解して1文字1マスで扱えるようにする - for (int i = 0; i < height; i++) { - final List list = new ArrayList<>(); - final String mapChar = inputData.mapStatus.get(i); - for (int j = 0; j < width; j++) { - list.add(mapChar.substring(j, j + 1)); + for(int i = 0; i < getHeight(); i++) { + String text = inputData.mapStatus[i]; + for(int j = 0; j < getWidth(); j++) { + getWorld()[i][j] = text.charAt(j); } - world.add(list); } } - + + public final int getHeight() { + return height; + } + + public int getWidth() { + return width; + } + + public final char[][] getWorld() { + return world; + } } diff --git a/sitou/test/A083_Test.java b/sitou/test/A083_Test.java index 5fc6371..c253510 100644 --- a/sitou/test/A083_Test.java +++ b/sitou/test/A083_Test.java @@ -2,7 +2,6 @@ package test; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; -import java.util.Arrays; import org.junit.Test; import src.A083_SecretMessage; import src.InputData; @@ -11,9 +10,12 @@ import src.TreasureMap; public class A083_Test { @Test - public void 正しく探索できるかテスト1() { + public void 正しく探索できるかテスト() { - final InputData inputData = new InputData(3,5,Arrays.asList("hello","paiza","world")); + final String[] mapStatus = { + "hello", "paiza", "world" + }; + final InputData inputData = new InputData(3, 5, mapStatus); TreasureMap treasureMap = new TreasureMap(inputData); final Result result = A083_SecretMessage.solve(treasureMap); String actual = result.getMessage(); @@ -23,9 +25,12 @@ public class A083_Test { assertThat(actual, is(expected)); } - public void 最も辞書順で小さいメッセージを探索できるかテスト() { + public void 辞書順で最も小さいメッセージを探索できるかテスト1() { - final InputData inputData = new InputData(3,3,Arrays.asList("abc","bcx","cyz")); + final String[] mapStatus = { + "abc", "bcx", "cyz" + }; + final InputData inputData = new InputData(3, 3, mapStatus); TreasureMap treasureMap = new TreasureMap(inputData); final Result result = A083_SecretMessage.solve(treasureMap); String actual = result.getMessage(); @@ -34,4 +39,19 @@ public class A083_Test { assertThat(actual, is(expected)); } + + public void 辞書順で最も小さいメッセージを探索できるかテスト2() { + + final String[] mapStatus = { + "xabcd", "abcde", "bddea", "cdeab", "deabc", "eabcd" + }; + final InputData inputData = new InputData(6, 5, mapStatus); + TreasureMap treasureMap = new TreasureMap(inputData); + final Result result = A083_SecretMessage.solve(treasureMap); + String actual = result.getMessage(); + + final String expected = "xababcdcddbcdeabababcd"; + + assertThat(actual, is(expected)); + } } diff --git a/sitou/test/TreasureMapTest.java b/sitou/test/TreasureMapTest.java index 6c52480..cbaaedf 100644 --- a/sitou/test/TreasureMapTest.java +++ b/sitou/test/TreasureMapTest.java @@ -1,73 +1,75 @@ package test; -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; +import src.InputData; +import src.TreasureMap; public class TreasureMapTest { @Test public void マスごとに正しく分割できるかテスト() { - final List> actual = new ArrayList>(); - final List mapStatus = Arrays.asList("hello", "paiza", "world"); - for (int i = 0; i < 3; i++) { - final List list = new ArrayList<>(); - final String mapChar = mapStatus.get(i); - for (int j = 0; j < 5; j++) { - list.add(mapChar.substring(j, j + 1)); - } - actual.add(list); - } + final String[] mapStatus = { + "hello", "paiza", "world" + }; + InputData input = new InputData(3, 5, mapStatus); + TreasureMap actual = new TreasureMap(input); - final List> expected = new ArrayList>(Arrays - .asList(Arrays.asList("h", "e", "l", "l", "o"), Arrays.asList("p", "a", "i", "z", "a"), - Arrays.asList("w", "o", "r", "l", "d"))); + final char[][] expected = { + { + 'h', 'e', 'l', 'l', 'o' + }, { + 'p', 'a', 'i', 'z', 'a' + }, { + 'w', 'o', 'r', 'l', 'd' + } + }; - assertThat(actual, is(expected)); + assertArrayEquals(actual.getWorld(), expected); } @Test public void 秘境の大きさが最小の時に正しく分割できるかテスト() { - final List> actual = new ArrayList>(); - final List mapStatus = Arrays.asList("a"); - for (int i = 0; i < 1; i++) { - final List list = new ArrayList<>(); - final String mapChar = mapStatus.get(i); - for (int j = 0; j < 1; j++) { - list.add(mapChar.substring(j, j + 1)); - } - actual.add(list); - } + final String[] mapStatus = { + "a" + }; + InputData input = new InputData(1, 1, mapStatus); + TreasureMap actual = new TreasureMap(input); - final List> expected = new ArrayList>(Arrays - .asList(Arrays.asList("a"))); + final char[][] expected = { + { + 'a' + } + }; - assertThat(actual, is(expected)); + assertArrayEquals(actual.getWorld(), expected); } @Test public void 秘境の大きさが最大の時に正しく分割できるかテスト() { - final List> actual = new ArrayList>(); - final List mapStatus = Arrays - .asList("abcde", "fghij", "klmno", "pqrst", "uvwxy", "zzzzz"); - for (int i = 0; i < 6; i++) { - final List list = new ArrayList<>(); - final String mapChar = mapStatus.get(i); - for (int j = 0; j < 5; j++) { - list.add(mapChar.substring(j, j + 1)); - } - actual.add(list); - } + final String[] mapStatus = { + "abcde", "fghij", "klmno", "pqrst", "uvwxy", "zzzzz" + }; + InputData input = new InputData(6, 5, mapStatus); + TreasureMap actual = new TreasureMap(input); - final List> expected = new ArrayList>(Arrays - .asList(Arrays.asList("a", "b", "c", "d", "e"), Arrays.asList("f", "g", "h", "i", "j"), - Arrays.asList("k", "l", "m", "n", "o"), Arrays.asList("p", "q", "r", "s", "t"), Arrays - .asList("u", "v", "w", "x", "y"), Arrays.asList("z", "z", "z", "z", "z"))); + final char[][] expected = { + { + 'a', 'b', 'c', 'd', 'e' + }, { + 'f', 'g', 'h', 'i', 'j' + }, { + 'k', 'l', 'm', 'n', 'o' + }, { + 'p', 'q', 'r', 's', 't' + }, { + 'u', 'v', 'w', 'x', 'y' + }, { + 'z', 'z', 'z', 'z', 'z' + } + }; - assertThat(actual, is(expected)); + assertArrayEquals(actual.getWorld(), expected); } } -- GitLab From 4701b5c0de0bde508a097dbcb6e39bc065496de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Tue, 13 Aug 2024 15:43:40 +0900 Subject: [PATCH 03/12] =?UTF-8?q?paiza=E3=81=AEA083=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/A083_SecretMessage.java | 53 ++++++++++++++++++++++--------- sitou/src/TreasureMap.java | 29 ++++++++++++----- 2 files changed, 59 insertions(+), 23 deletions(-) diff --git a/sitou/src/A083_SecretMessage.java b/sitou/src/A083_SecretMessage.java index afe5e3a..5d93b58 100644 --- a/sitou/src/A083_SecretMessage.java +++ b/sitou/src/A083_SecretMessage.java @@ -33,11 +33,11 @@ public class A083_SecretMessage { } public static Result solve(TreasureMap treasureMap) { - boolean[][] visited = new boolean[treasureMap.getHeight()][treasureMap.getWidth()]; + List allPaths = new ArrayList<>(); - findPaths(0, 0, "", visited, treasureMap, allPaths); + findPaths(0, 0, "", treasureMap, allPaths); - //辞書順で早いものを探索 + // 辞書順で早いものを探索 String smallestPath = allPaths.get(0); for (String p : allPaths) { if (p.compareTo(smallestPath) < 0) { @@ -47,32 +47,55 @@ public class A083_SecretMessage { return new Result(smallestPath); } + + public enum Direction{ + UP(0, -1), + DOWN(0,1), + LEFT(-1,0), + RIGHT(1,0); + + private final int x; + private final int y; + + private Direction(final int x, final int y) { + this.x = x; + this.y = y; + } + + public final int getX() { + return x; + } + + public final int getY() { + return y; + } + } - public static void findPaths(int x, int y, String path, boolean[][] visited, + public static void findPaths(int x, int y, String path, TreasureMap treasureMap, List allPaths) { - //上下左右確認用 - int[] dx = {-1, 1, 0, 0}; - int[] dy = {0, 0, -1, 1}; - //Map外や二度目に通る所のとき - if (x < 0 || y < 0 || x >= treasureMap.getWorld().length || y >= treasureMap.getWorld()[0].length || visited[x][y]) { + // Map外や二度目に通る所のとき + if (x < 0 || y < 0 || x >= treasureMap.getHeight() || y >= treasureMap.getWidth() + || treasureMap.visited[x][y]) { return; } - //ゴールのとき + // ゴールのとき if (x == treasureMap.getWorld().length - 1 && y == treasureMap.getWorld()[0].length - 1) { allPaths.add(path + treasureMap.getWorld()[x][y]); return; } - visited[x][y] = true; - path += treasureMap.getWorld()[x][y]; + treasureMap.mark(x, y); - for (int i = 0; i < 4; i++) { - findPaths(x + dx[i], y + dy[i], path, visited, treasureMap, allPaths); + path += treasureMap.getWorld()[x][y]; + + for(Direction direction : Direction.values()) { + findPaths(x + direction.getX(), y + direction.getY(), path, treasureMap, allPaths); } - visited[x][y] = false; + treasureMap.erase(x, y); + } private static void output(final Result result) { diff --git a/sitou/src/TreasureMap.java b/sitou/src/TreasureMap.java index 29f9b7c..ccf2571 100644 --- a/sitou/src/TreasureMap.java +++ b/sitou/src/TreasureMap.java @@ -2,33 +2,46 @@ package src; public class TreasureMap { final int height; - private final int width; + final int width; final char[][] world; + final boolean[][] visited; public TreasureMap(InputData inputData) { height = inputData.height; width = inputData.width; world = new char[getHeight()][getWidth()]; + visited = new boolean[getHeight()][getWidth()]; - - //scanした文字列を分解して1文字1マスで扱えるようにする - for(int i = 0; i < getHeight(); i++) { + // scanした文字列を分解して1文字1マスで扱えるようにする + for (int i = 0; i < getHeight(); i++) { String text = inputData.mapStatus[i]; - for(int j = 0; j < getWidth(); j++) { + for (int j = 0; j < getWidth(); j++) { getWorld()[i][j] = text.charAt(j); } } } - + + public void mark(final int x, final int y) { + visited[x][y] = true; + } + + public void erase(final int x, final int y) { + visited[x][y] = false; + } + public final int getHeight() { return height; } - + public int getWidth() { return width; } - + public final char[][] getWorld() { return world; } + + public final boolean[][] getVisited() { + return visited; + } } -- GitLab From 8dad2e92dd54900964e2977d16dd9a6d23c27c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Tue, 13 Aug 2024 16:31:30 +0900 Subject: [PATCH 04/12] =?UTF-8?q?treasureMap.get(0,0)=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/A083_SecretMessage.java | 1 + sitou/src/TreasureMap.java | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/sitou/src/A083_SecretMessage.java b/sitou/src/A083_SecretMessage.java index 5d93b58..7419321 100644 --- a/sitou/src/A083_SecretMessage.java +++ b/sitou/src/A083_SecretMessage.java @@ -33,6 +33,7 @@ public class A083_SecretMessage { } public static Result solve(TreasureMap treasureMap) { + treasureMap.get(0,0); List allPaths = new ArrayList<>(); findPaths(0, 0, "", treasureMap, allPaths); diff --git a/sitou/src/TreasureMap.java b/sitou/src/TreasureMap.java index ccf2571..e8d2fea 100644 --- a/sitou/src/TreasureMap.java +++ b/sitou/src/TreasureMap.java @@ -44,4 +44,9 @@ public class TreasureMap { public final boolean[][] getVisited() { return visited; } + + public final char get(int x, int y) { + return world[x][y]; + + } } -- GitLab From 6d257a3920aa4d0a7277f2d551982d6fd39808f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Tue, 13 Aug 2024 16:48:19 +0900 Subject: [PATCH 05/12] =?UTF-8?q?isVisited=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/A083_SecretMessage.java | 26 +++++++++++++------------- sitou/src/TreasureMap.java | 8 ++++---- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/sitou/src/A083_SecretMessage.java b/sitou/src/A083_SecretMessage.java index 7419321..460844d 100644 --- a/sitou/src/A083_SecretMessage.java +++ b/sitou/src/A083_SecretMessage.java @@ -33,7 +33,7 @@ public class A083_SecretMessage { } public static Result solve(TreasureMap treasureMap) { - treasureMap.get(0,0); + treasureMap.get(0, 0); List allPaths = new ArrayList<>(); findPaths(0, 0, "", treasureMap, allPaths); @@ -48,25 +48,25 @@ public class A083_SecretMessage { return new Result(smallestPath); } - - public enum Direction{ + + public enum Direction { UP(0, -1), - DOWN(0,1), - LEFT(-1,0), - RIGHT(1,0); - + DOWN(0, 1), + LEFT(-1, 0), + RIGHT(1, 0); + private final int x; private final int y; - + private Direction(final int x, final int y) { this.x = x; this.y = y; } - + public final int getX() { return x; } - + public final int getY() { return y; } @@ -77,7 +77,7 @@ public class A083_SecretMessage { // Map外や二度目に通る所のとき if (x < 0 || y < 0 || x >= treasureMap.getHeight() || y >= treasureMap.getWidth() - || treasureMap.visited[x][y]) { + || treasureMap.isVisited(x, y)) { return; } @@ -90,8 +90,8 @@ public class A083_SecretMessage { treasureMap.mark(x, y); path += treasureMap.getWorld()[x][y]; - - for(Direction direction : Direction.values()) { + + for (Direction direction : Direction.values()) { findPaths(x + direction.getX(), y + direction.getY(), path, treasureMap, allPaths); } diff --git a/sitou/src/TreasureMap.java b/sitou/src/TreasureMap.java index e8d2fea..0e1fef0 100644 --- a/sitou/src/TreasureMap.java +++ b/sitou/src/TreasureMap.java @@ -41,12 +41,12 @@ public class TreasureMap { return world; } - public final boolean[][] getVisited() { - return visited; + public final boolean isVisited(final int x, final int y) { + return visited[x][y]; } - public final char get(int x, int y) { + public final char get(final int x, final int y) { return world[x][y]; - + } } -- GitLab From 4d0c1c1815ed696c0e29ae023b2253b7c4ac4fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Wed, 14 Aug 2024 14:53:04 +0900 Subject: [PATCH 06/12] =?UTF-8?q?paiza=E3=81=AEB015=E3=81=AE=E5=9B=9E?= =?UTF-8?q?=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/A083_SecretMessage.java | 106 ---------------------- sitou/src/B015_7segment.java | 145 ++++++++++++++++++++++++++++++ sitou/src/InputData.java | 24 +++-- sitou/src/Result.java | 12 +-- sitou/src/TreasureMap.java | 52 ----------- sitou/test/A083_Test.java | 57 ------------ sitou/test/B015_Test.java | 56 ++++++++++++ sitou/test/TreasureMapTest.java | 75 ---------------- 8 files changed, 224 insertions(+), 303 deletions(-) delete mode 100644 sitou/src/A083_SecretMessage.java create mode 100644 sitou/src/B015_7segment.java delete mode 100644 sitou/src/TreasureMap.java delete mode 100644 sitou/test/A083_Test.java create mode 100644 sitou/test/B015_Test.java delete mode 100644 sitou/test/TreasureMapTest.java diff --git a/sitou/src/A083_SecretMessage.java b/sitou/src/A083_SecretMessage.java deleted file mode 100644 index 460844d..0000000 --- a/sitou/src/A083_SecretMessage.java +++ /dev/null @@ -1,106 +0,0 @@ -package src; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class A083_SecretMessage { - - public static void main(String[] args) { - execute(); - } - - static void execute() { - final InputData inputData = input(); - final TreasureMap treasureMap = new TreasureMap(inputData); - final Result result = solve(treasureMap); - output(result); - } - - private static InputData input() { - final Scanner scan = new Scanner(System.in); - final int height = scan.nextInt(); - final int width = scan.nextInt(); - final String[] mapStatus = new String[height]; - - for (int i = 0; i < height; i++) { - mapStatus[i] = scan.next(); - } - - scan.close(); - return new InputData(height, width, mapStatus); - - } - - public static Result solve(TreasureMap treasureMap) { - treasureMap.get(0, 0); - - List allPaths = new ArrayList<>(); - findPaths(0, 0, "", treasureMap, allPaths); - - // 辞書順で早いものを探索 - String smallestPath = allPaths.get(0); - for (String p : allPaths) { - if (p.compareTo(smallestPath) < 0) { - smallestPath = p; - } - } - return new Result(smallestPath); - - } - - public enum Direction { - UP(0, -1), - DOWN(0, 1), - LEFT(-1, 0), - RIGHT(1, 0); - - private final int x; - private final int y; - - private Direction(final int x, final int y) { - this.x = x; - this.y = y; - } - - public final int getX() { - return x; - } - - public final int getY() { - return y; - } - } - - public static void findPaths(int x, int y, String path, - TreasureMap treasureMap, List allPaths) { - - // Map外や二度目に通る所のとき - if (x < 0 || y < 0 || x >= treasureMap.getHeight() || y >= treasureMap.getWidth() - || treasureMap.isVisited(x, y)) { - return; - } - - // ゴールのとき - if (x == treasureMap.getWorld().length - 1 && y == treasureMap.getWorld()[0].length - 1) { - allPaths.add(path + treasureMap.getWorld()[x][y]); - return; - } - - treasureMap.mark(x, y); - - path += treasureMap.getWorld()[x][y]; - - for (Direction direction : Direction.values()) { - findPaths(x + direction.getX(), y + direction.getY(), path, treasureMap, allPaths); - } - - treasureMap.erase(x, y); - - } - - private static void output(final Result result) { - System.out.println(result.getMessage()); - } - -} diff --git a/sitou/src/B015_7segment.java b/sitou/src/B015_7segment.java new file mode 100644 index 0000000..f091e08 --- /dev/null +++ b/sitou/src/B015_7segment.java @@ -0,0 +1,145 @@ +package src; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +public class B015_7segment { + public static void main(String[] args) { + execute(); + } + + static void execute() { + final InputData inputData = input(); + final Result result = solveLightNumber(inputData); + output(result); + } + + private static InputData input() { + final Scanner scan = new Scanner(System.in); + final List leftSegment = new ArrayList<>(); + final List rightSegment = new ArrayList<>(); + + try { + for (int i = 0; i < 7; i++) { + leftSegment.add(scan.nextInt()); + } + for (int i = 0; i < 7; i++) { + rightSegment.add(scan.nextInt()); + } + } finally { + scan.close(); + } + + return new InputData(leftSegment, rightSegment); + + } + + public static Result solveLightNumber(InputData inputData) { + final List result = new ArrayList<>(); + + result.add(checkWhetherNumber(inputData)); + + // 対称移動 + InputData insideOutData = insideOut(inputData); + result.add(checkWhetherNumber(insideOutData)); + + // 対称移動+上下反転=回転移動 + InputData upsideDownData = upsideDown(insideOutData); + result.add(checkWhetherNumber(upsideDownData)); + + return new Result(result); + + } + + public static String checkWhetherNumber(InputData inputData) { + // リストを1つの文字列にする + String ls = ""; + String rs = ""; + for (Integer n : inputData.getLeftSegment()) { + ls += n; + } + for (Integer n : inputData.getRightSegment()) { + rs += n; + } + + final boolean isNumberLeft = isNumber(ls); + final boolean isNumberRight = isNumber(rs); + + String isNumberBoth = "No"; + if (isNumberLeft && isNumberRight) { + isNumberBoth = "Yes"; + } + + return isNumberBoth; + } + + private static boolean isNumber(final String seg) { + for (NumberPatterns numberPattern : NumberPatterns.values()) { + if (seg.equals(numberPattern.getPattern())) { + return true; + } + } + return false; + } + + public static InputData insideOut(final InputData inputData) { + List leftSegment = new ArrayList<>(inputData.getLeftSegment()); + List rightSegment = new ArrayList<>(inputData.getRightSegment()); + Collections.swap(leftSegment, 1, 5); + Collections.swap(leftSegment, 2, 4); + Collections.swap(rightSegment, 1, 5); + Collections.swap(rightSegment, 2, 4); + + List kari = new ArrayList<>(leftSegment); + leftSegment = new ArrayList<>(rightSegment); + rightSegment = new ArrayList<>(kari); + + return new InputData(leftSegment, rightSegment); + } + + public static InputData upsideDown(final InputData inputData) { + List leftSegment = new ArrayList<>(inputData.getLeftSegment()); + List rightSegment = new ArrayList<>(inputData.getRightSegment()); + Collections.swap(leftSegment, 0, 3); + Collections.swap(leftSegment, 1, 2); + Collections.swap(leftSegment, 4, 5); + Collections.swap(rightSegment, 0, 3); + Collections.swap(rightSegment, 1, 2); + Collections.swap(rightSegment, 4, 5); + + return new InputData(leftSegment, rightSegment); + } + + public enum NumberPatterns { + // 各数字に対応する7セグメントディスプレイの状態 + ZERO("1111110"), + ONE("0110000"), + TWO("1101101"), + THREE("1111001"), + FOUR("0110011"), + FIVE("1011011"), + SIX("1011111"), + SEVEN("1110010"), + EIGHT("1111111"), + NINE("1111011"); + + private final String pattern; + + private NumberPatterns(final String pattern) { + this.pattern = pattern; + } + + public final String getPattern() { + return pattern; + } + } + + private static void output(final Result result) { + for (int i = 0; i < 3; i++) { + System.out.println(result.getIsNumber().get(i)); + } + } + +} diff --git a/sitou/src/InputData.java b/sitou/src/InputData.java index fd402e3..19809a1 100644 --- a/sitou/src/InputData.java +++ b/sitou/src/InputData.java @@ -1,14 +1,22 @@ package src; +import java.util.List; + public class InputData { - final int height; - final int width; - final String[] mapStatus; - - public InputData(final int height, final int width, final String[] mapStatus) { - this.height = height; - this.width = width; - this.mapStatus = mapStatus; + private final List leftSegment; + private final List rightSegment; + + public InputData(final List leftSegment, final List rightSegment) { + this.leftSegment = leftSegment; + this.rightSegment = rightSegment; + } + + public final List getLeftSegment() { + return leftSegment; + } + + public final List getRightSegment() { + return rightSegment; } } diff --git a/sitou/src/Result.java b/sitou/src/Result.java index 8d026fd..fcc302e 100644 --- a/sitou/src/Result.java +++ b/sitou/src/Result.java @@ -1,13 +1,15 @@ package src; +import java.util.List; + public class Result { - final String message; + final List isNumber; - Result(final String message) { - this.message = message; + Result(final List isNumber) { + this.isNumber = isNumber; } - public final String getMessage() { - return message; + public final List getIsNumber() { + return isNumber; } } diff --git a/sitou/src/TreasureMap.java b/sitou/src/TreasureMap.java deleted file mode 100644 index 0e1fef0..0000000 --- a/sitou/src/TreasureMap.java +++ /dev/null @@ -1,52 +0,0 @@ -package src; - -public class TreasureMap { - final int height; - final int width; - final char[][] world; - final boolean[][] visited; - - public TreasureMap(InputData inputData) { - height = inputData.height; - width = inputData.width; - world = new char[getHeight()][getWidth()]; - visited = new boolean[getHeight()][getWidth()]; - - // scanした文字列を分解して1文字1マスで扱えるようにする - for (int i = 0; i < getHeight(); i++) { - String text = inputData.mapStatus[i]; - for (int j = 0; j < getWidth(); j++) { - getWorld()[i][j] = text.charAt(j); - } - } - } - - public void mark(final int x, final int y) { - visited[x][y] = true; - } - - public void erase(final int x, final int y) { - visited[x][y] = false; - } - - public final int getHeight() { - return height; - } - - public int getWidth() { - return width; - } - - public final char[][] getWorld() { - return world; - } - - public final boolean isVisited(final int x, final int y) { - return visited[x][y]; - } - - public final char get(final int x, final int y) { - return world[x][y]; - - } -} diff --git a/sitou/test/A083_Test.java b/sitou/test/A083_Test.java deleted file mode 100644 index c253510..0000000 --- a/sitou/test/A083_Test.java +++ /dev/null @@ -1,57 +0,0 @@ -package test; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import org.junit.Test; -import src.A083_SecretMessage; -import src.InputData; -import src.Result; -import src.TreasureMap; - -public class A083_Test { - @Test - public void 正しく探索できるかテスト() { - - final String[] mapStatus = { - "hello", "paiza", "world" - }; - final InputData inputData = new InputData(3, 5, mapStatus); - TreasureMap treasureMap = new TreasureMap(inputData); - final Result result = A083_SecretMessage.solve(treasureMap); - String actual = result.getMessage(); - - final String expected = "heailload"; - - assertThat(actual, is(expected)); - } - - public void 辞書順で最も小さいメッセージを探索できるかテスト1() { - - final String[] mapStatus = { - "abc", "bcx", "cyz" - }; - final InputData inputData = new InputData(3, 3, mapStatus); - TreasureMap treasureMap = new TreasureMap(inputData); - final Result result = A083_SecretMessage.solve(treasureMap); - String actual = result.getMessage(); - - final String expected = "abcbcxz"; - - assertThat(actual, is(expected)); - } - - public void 辞書順で最も小さいメッセージを探索できるかテスト2() { - - final String[] mapStatus = { - "xabcd", "abcde", "bddea", "cdeab", "deabc", "eabcd" - }; - final InputData inputData = new InputData(6, 5, mapStatus); - TreasureMap treasureMap = new TreasureMap(inputData); - final Result result = A083_SecretMessage.solve(treasureMap); - String actual = result.getMessage(); - - final String expected = "xababcdcddbcdeabababcd"; - - assertThat(actual, is(expected)); - } -} diff --git a/sitou/test/B015_Test.java b/sitou/test/B015_Test.java new file mode 100644 index 0000000..12fe4ec --- /dev/null +++ b/sitou/test/B015_Test.java @@ -0,0 +1,56 @@ +package test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import src.B015_7segment; +import src.InputData; +import src.Result; + +public class B015_Test { + @Test + public void 正しく判定できるかテスト() { + final InputData inputData = new InputData(Arrays.asList(1, 1, 1, 1, 0, 1, 1), Arrays + .asList(0, 1, 1, 0, 0, 0, 0)); + final Result result = B015_7segment.solveLightNumber(inputData); + final List actual = result.getIsNumber(); + + final List expected = Arrays.asList("Yes", "No", "No"); + + assertThat(actual, is(expected)); + } + + @Test + public void 正しく表裏反転できるかテスト() { + InputData inputData = new InputData(Arrays.asList(1, 1, 1, 1, 0, 1, 1), Arrays + .asList(0, 1, 1, 0, 0, 0, 0)); + InputData insideOutData = B015_7segment.insideOut(inputData); + List actualLeft = new ArrayList<>(insideOutData.getLeftSegment()); + List actualRight = new ArrayList<>(insideOutData.getRightSegment()); + + List expectedLeft = Arrays.asList(0, 0, 0, 0, 1, 1, 0); + List expectedRight = Arrays.asList(1, 1, 0, 1, 1, 1, 1); + + assertThat(actualLeft, is(expectedLeft)); + assertThat(actualRight, is(expectedRight)); + } + + @Test + public void 正しく上下反転できるかテスト() { + InputData inputData = new InputData(Arrays.asList(0, 0, 0, 0, 1, 1, 0), Arrays + .asList(1, 1, 0, 1, 1, 1, 1)); + InputData upsideDownData = B015_7segment.upsideDown(inputData); + List actualLeft = new ArrayList<>(upsideDownData.getLeftSegment()); + List actualRight = new ArrayList<>(upsideDownData.getRightSegment()); + + List expectedLeft = Arrays.asList(0, 0, 0, 0, 1, 1, 0); + List expectedRight = Arrays.asList(1, 0, 1, 1, 1, 1, 1); + + assertThat(actualLeft, is(expectedLeft)); + assertThat(actualRight, is(expectedRight)); + } + +} diff --git a/sitou/test/TreasureMapTest.java b/sitou/test/TreasureMapTest.java deleted file mode 100644 index cbaaedf..0000000 --- a/sitou/test/TreasureMapTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package test; - -import static org.junit.Assert.*; -import org.junit.Test; -import src.InputData; -import src.TreasureMap; - -public class TreasureMapTest { - @Test - public void マスごとに正しく分割できるかテスト() { - - final String[] mapStatus = { - "hello", "paiza", "world" - }; - InputData input = new InputData(3, 5, mapStatus); - TreasureMap actual = new TreasureMap(input); - - final char[][] expected = { - { - 'h', 'e', 'l', 'l', 'o' - }, { - 'p', 'a', 'i', 'z', 'a' - }, { - 'w', 'o', 'r', 'l', 'd' - } - }; - - assertArrayEquals(actual.getWorld(), expected); - - } - - @Test - public void 秘境の大きさが最小の時に正しく分割できるかテスト() { - final String[] mapStatus = { - "a" - }; - InputData input = new InputData(1, 1, mapStatus); - TreasureMap actual = new TreasureMap(input); - - final char[][] expected = { - { - 'a' - } - }; - - assertArrayEquals(actual.getWorld(), expected); - } - - @Test - public void 秘境の大きさが最大の時に正しく分割できるかテスト() { - final String[] mapStatus = { - "abcde", "fghij", "klmno", "pqrst", "uvwxy", "zzzzz" - }; - InputData input = new InputData(6, 5, mapStatus); - TreasureMap actual = new TreasureMap(input); - - final char[][] expected = { - { - 'a', 'b', 'c', 'd', 'e' - }, { - 'f', 'g', 'h', 'i', 'j' - }, { - 'k', 'l', 'm', 'n', 'o' - }, { - 'p', 'q', 'r', 's', 't' - }, { - 'u', 'v', 'w', 'x', 'y' - }, { - 'z', 'z', 'z', 'z', 'z' - } - }; - - assertArrayEquals(actual.getWorld(), expected); - } -} -- GitLab From 8bc00a349fd0ade40e20b933c7e9eb850d48c3f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Tue, 27 Aug 2024 11:55:02 +0900 Subject: [PATCH 07/12] =?UTF-8?q?B015=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B015_7segment.java | 39 ++++++++++++++++++++++++++++++++++-- sitou/test/B015_Test.java | 2 +- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/sitou/src/B015_7segment.java b/sitou/src/B015_7segment.java index f091e08..ea72e4f 100644 --- a/sitou/src/B015_7segment.java +++ b/sitou/src/B015_7segment.java @@ -12,7 +12,7 @@ public class B015_7segment { static void execute() { final InputData inputData = input(); - final Result result = solveLightNumber(inputData); + final Result result = storeJudgementResult(inputData); output(result); } @@ -36,7 +36,13 @@ public class B015_7segment { } - public static Result solveLightNumber(InputData inputData) { + /** + * 結果を格納する. + * + * @param inputData 入力データ + * @return 結果を格納したデータ + */ + public static Result storeJudgementResult(InputData inputData) { final List result = new ArrayList<>(); result.add(checkWhetherNumber(inputData)); @@ -53,6 +59,12 @@ public class B015_7segment { } + /** + * 数字かどうか判定した結果(Yes/No)を返す. + * + * @param inputData 入力データ + * @return "Yes"/"No" + */ public static String checkWhetherNumber(InputData inputData) { // リストを1つの文字列にする String ls = ""; @@ -75,6 +87,12 @@ public class B015_7segment { return isNumberBoth; } + /** + * 数字かどうか判定する. + * + * @param seg セグメントディスプレイの状態(数字7桁) + * @return true/false + */ private static boolean isNumber(final String seg) { for (NumberPatterns numberPattern : NumberPatterns.values()) { if (seg.equals(numberPattern.getPattern())) { @@ -84,6 +102,12 @@ public class B015_7segment { return false; } + /** + * セグメントディスプレイの表示を対称移動する. + * + * @param inputData 入力データ + * @return セグメントディスプレイの状態 + */ public static InputData insideOut(final InputData inputData) { List leftSegment = new ArrayList<>(inputData.getLeftSegment()); List rightSegment = new ArrayList<>(inputData.getRightSegment()); @@ -99,6 +123,12 @@ public class B015_7segment { return new InputData(leftSegment, rightSegment); } + /** + * セグメントディスプレイの表示を上下反転する. + * + * @param inputData 入力データ + * @return セグメントディスプレイの状態 + */ public static InputData upsideDown(final InputData inputData) { List leftSegment = new ArrayList<>(inputData.getLeftSegment()); List rightSegment = new ArrayList<>(inputData.getRightSegment()); @@ -136,6 +166,11 @@ public class B015_7segment { } } + /** + * 結果を出力する. + * + * @param result 結果を格納したデータ + */ private static void output(final Result result) { for (int i = 0; i < 3; i++) { System.out.println(result.getIsNumber().get(i)); diff --git a/sitou/test/B015_Test.java b/sitou/test/B015_Test.java index 12fe4ec..8a18bcf 100644 --- a/sitou/test/B015_Test.java +++ b/sitou/test/B015_Test.java @@ -15,7 +15,7 @@ public class B015_Test { public void 正しく判定できるかテスト() { final InputData inputData = new InputData(Arrays.asList(1, 1, 1, 1, 0, 1, 1), Arrays .asList(0, 1, 1, 0, 0, 0, 0)); - final Result result = B015_7segment.solveLightNumber(inputData); + final Result result = B015_7segment.storeJudgementResult(inputData); final List actual = result.getIsNumber(); final List expected = Arrays.asList("Yes", "No", "No"); -- GitLab From 189aacca993bfc5ca7c42219f00635700ece5a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Wed, 28 Aug 2024 10:16:20 +0900 Subject: [PATCH 08/12] =?UTF-8?q?paiza=E3=81=AEB118=E3=81=AE=E5=9B=9E?= =?UTF-8?q?=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B015_7segment.java | 180 ----------------------------------- sitou/src/B118.java | 122 ++++++++++++++++++++++++ sitou/src/InputData.java | 22 ----- sitou/src/Result.java | 15 --- sitou/src/Student.java | 31 ++++++ sitou/test/B015_Test.java | 56 ----------- sitou/test/B118_Test.java | 88 +++++++++++++++++ 7 files changed, 241 insertions(+), 273 deletions(-) delete mode 100644 sitou/src/B015_7segment.java create mode 100644 sitou/src/B118.java delete mode 100644 sitou/src/InputData.java delete mode 100644 sitou/src/Result.java create mode 100644 sitou/src/Student.java delete mode 100644 sitou/test/B015_Test.java create mode 100644 sitou/test/B118_Test.java diff --git a/sitou/src/B015_7segment.java b/sitou/src/B015_7segment.java deleted file mode 100644 index ea72e4f..0000000 --- a/sitou/src/B015_7segment.java +++ /dev/null @@ -1,180 +0,0 @@ -package src; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Scanner; - -public class B015_7segment { - public static void main(String[] args) { - execute(); - } - - static void execute() { - final InputData inputData = input(); - final Result result = storeJudgementResult(inputData); - output(result); - } - - private static InputData input() { - final Scanner scan = new Scanner(System.in); - final List leftSegment = new ArrayList<>(); - final List rightSegment = new ArrayList<>(); - - try { - for (int i = 0; i < 7; i++) { - leftSegment.add(scan.nextInt()); - } - for (int i = 0; i < 7; i++) { - rightSegment.add(scan.nextInt()); - } - } finally { - scan.close(); - } - - return new InputData(leftSegment, rightSegment); - - } - - /** - * 結果を格納する. - * - * @param inputData 入力データ - * @return 結果を格納したデータ - */ - public static Result storeJudgementResult(InputData inputData) { - final List result = new ArrayList<>(); - - result.add(checkWhetherNumber(inputData)); - - // 対称移動 - InputData insideOutData = insideOut(inputData); - result.add(checkWhetherNumber(insideOutData)); - - // 対称移動+上下反転=回転移動 - InputData upsideDownData = upsideDown(insideOutData); - result.add(checkWhetherNumber(upsideDownData)); - - return new Result(result); - - } - - /** - * 数字かどうか判定した結果(Yes/No)を返す. - * - * @param inputData 入力データ - * @return "Yes"/"No" - */ - public static String checkWhetherNumber(InputData inputData) { - // リストを1つの文字列にする - String ls = ""; - String rs = ""; - for (Integer n : inputData.getLeftSegment()) { - ls += n; - } - for (Integer n : inputData.getRightSegment()) { - rs += n; - } - - final boolean isNumberLeft = isNumber(ls); - final boolean isNumberRight = isNumber(rs); - - String isNumberBoth = "No"; - if (isNumberLeft && isNumberRight) { - isNumberBoth = "Yes"; - } - - return isNumberBoth; - } - - /** - * 数字かどうか判定する. - * - * @param seg セグメントディスプレイの状態(数字7桁) - * @return true/false - */ - private static boolean isNumber(final String seg) { - for (NumberPatterns numberPattern : NumberPatterns.values()) { - if (seg.equals(numberPattern.getPattern())) { - return true; - } - } - return false; - } - - /** - * セグメントディスプレイの表示を対称移動する. - * - * @param inputData 入力データ - * @return セグメントディスプレイの状態 - */ - public static InputData insideOut(final InputData inputData) { - List leftSegment = new ArrayList<>(inputData.getLeftSegment()); - List rightSegment = new ArrayList<>(inputData.getRightSegment()); - Collections.swap(leftSegment, 1, 5); - Collections.swap(leftSegment, 2, 4); - Collections.swap(rightSegment, 1, 5); - Collections.swap(rightSegment, 2, 4); - - List kari = new ArrayList<>(leftSegment); - leftSegment = new ArrayList<>(rightSegment); - rightSegment = new ArrayList<>(kari); - - return new InputData(leftSegment, rightSegment); - } - - /** - * セグメントディスプレイの表示を上下反転する. - * - * @param inputData 入力データ - * @return セグメントディスプレイの状態 - */ - public static InputData upsideDown(final InputData inputData) { - List leftSegment = new ArrayList<>(inputData.getLeftSegment()); - List rightSegment = new ArrayList<>(inputData.getRightSegment()); - Collections.swap(leftSegment, 0, 3); - Collections.swap(leftSegment, 1, 2); - Collections.swap(leftSegment, 4, 5); - Collections.swap(rightSegment, 0, 3); - Collections.swap(rightSegment, 1, 2); - Collections.swap(rightSegment, 4, 5); - - return new InputData(leftSegment, rightSegment); - } - - public enum NumberPatterns { - // 各数字に対応する7セグメントディスプレイの状態 - ZERO("1111110"), - ONE("0110000"), - TWO("1101101"), - THREE("1111001"), - FOUR("0110011"), - FIVE("1011011"), - SIX("1011111"), - SEVEN("1110010"), - EIGHT("1111111"), - NINE("1111011"); - - private final String pattern; - - private NumberPatterns(final String pattern) { - this.pattern = pattern; - } - - public final String getPattern() { - return pattern; - } - } - - /** - * 結果を出力する. - * - * @param result 結果を格納したデータ - */ - private static void output(final Result result) { - for (int i = 0; i < 3; i++) { - System.out.println(result.getIsNumber().get(i)); - } - } - -} diff --git a/sitou/src/B118.java b/sitou/src/B118.java new file mode 100644 index 0000000..bcaa1ea --- /dev/null +++ b/sitou/src/B118.java @@ -0,0 +1,122 @@ +package src; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class B118 { + private static int studentsNum; + private static final List studentsList = new ArrayList<>(); + private PrintStream out; + + public B118(InputStream in, PrintStream out) { + final Scanner scan = new Scanner(in); + + try { + studentsNum = scan.nextInt(); + for (int n = 0; n < studentsNum; n++) { + final String name = scan.next(); + final int height = scan.nextInt(); + final int birthMonth = scan.nextInt(); + final Student student = new Student(name, height, birthMonth); + studentsList.add(student); + } + + } finally { + scan.close(); + } + this.out = out; + } + + public static void main(String[] args) throws CloneNotSupportedException { + final B118 b118 = new B118(System.in, System.out); + b118.execute(); + } + + public void execute() throws CloneNotSupportedException { + final List result = sortHeight(); + output(result, out); + } + + /** + * 生徒を背の順にしてデータを格納する. + * + * @return 格納したデータ + */ + public static List sortHeight() throws CloneNotSupportedException { + final List result = new ArrayList<>(); + + // findSmallestStudentで見つけた生徒はstudentsListから削除する + // 全員確認する、つまりstudentsListが空になるまで繰り返す + while (studentsList.size() > 0) { + // 与えられた条件で最後尾になる生徒情報を初期値にする。 + final Student smallestStudent = new Student("zzzzzzzzzz", 200, 1); + result.add(findSmallestStudent(smallestStudent)); + } + return result; + } + + /** + * 残っている生徒の中で、背の順の最も小さい生徒を探す. + * + * @param smallestStudent 問題で与えられた条件で最後尾になる生徒情報 + * @return 最小の生徒の名前 + */ + public static String findSmallestStudent(Student smallestStudent) + throws CloneNotSupportedException { + // 残っているstudentsListの中で最小の生徒名を返す + // 該当の生徒はstudentsListから削除する + int studentNum = 0; + for (int i = 0; i < studentsList.size(); i++) { + Student studentToCheck = studentsList.get(i).clone(); + + if (isSmaller(smallestStudent, studentToCheck)) { + smallestStudent = studentToCheck.clone(); + studentNum = i; + } + } + studentsList.remove(studentNum); + + return smallestStudent.getName(); + } + + /** + * 問題で与えられた条件で生徒を比較する. + * 条件 + * ・背の低い順番に前から並ばせる + * ・背の高さが同じ場合、誕生月の数字が大きい方を前にする + * ・背の高さが同じでかつ誕生月が同じ場合、名前を辞書順で並べた時、早い方の人を前にする + * + * @param smallestStudent 現在最小と思われる生徒 + * @param studentToCheck 比較対象の生徒 + * @return true/false + */ + public static boolean isSmaller(final Student smallestStudent, final Student studentToCheck) { + if (smallestStudent.getHeight() > studentToCheck.getHeight()) { + return true; + } else if (smallestStudent.getHeight() == studentToCheck.getHeight()) { + if (smallestStudent.getBirthMonth() < studentToCheck.getBirthMonth()) { + return true; + } else if (smallestStudent.getBirthMonth() == studentToCheck.getBirthMonth()) { + return smallestStudent.getName().compareTo(studentToCheck.getName()) > 0; + } + } + return false; + } + + private static void output(final List result, final PrintStream out) { + for (int i = 0; i < result.size(); i++) { + out.println(result.get(i)); + } + } + + public static List getStudentsList() { + return studentsList; + } + + public static void resetStudentsList() { + studentsList.clear(); + } +} diff --git a/sitou/src/InputData.java b/sitou/src/InputData.java deleted file mode 100644 index 19809a1..0000000 --- a/sitou/src/InputData.java +++ /dev/null @@ -1,22 +0,0 @@ -package src; - -import java.util.List; - -public class InputData { - private final List leftSegment; - private final List rightSegment; - - public InputData(final List leftSegment, final List rightSegment) { - this.leftSegment = leftSegment; - this.rightSegment = rightSegment; - } - - public final List getLeftSegment() { - return leftSegment; - } - - public final List getRightSegment() { - return rightSegment; - } - -} diff --git a/sitou/src/Result.java b/sitou/src/Result.java deleted file mode 100644 index fcc302e..0000000 --- a/sitou/src/Result.java +++ /dev/null @@ -1,15 +0,0 @@ -package src; - -import java.util.List; - -public class Result { - final List isNumber; - - Result(final List isNumber) { - this.isNumber = isNumber; - } - - public final List getIsNumber() { - return isNumber; - } -} diff --git a/sitou/src/Student.java b/sitou/src/Student.java new file mode 100644 index 0000000..2a3a09d --- /dev/null +++ b/sitou/src/Student.java @@ -0,0 +1,31 @@ +package src; + +public class Student implements Cloneable { + private final String name; + private final int height; + private final int birthMonth; + + public Student(final String name, final int height, final int birthMonth) { + this.name = name; + this.height = height; + this.birthMonth = birthMonth; + } + + public String getName() { + return name; + } + + public int getHeight() { + return height; + } + + public int getBirthMonth() { + return birthMonth; + } + + @Override + public Student clone() throws CloneNotSupportedException { + Student clone = (Student) super.clone(); + return clone; + } +} diff --git a/sitou/test/B015_Test.java b/sitou/test/B015_Test.java deleted file mode 100644 index 8a18bcf..0000000 --- a/sitou/test/B015_Test.java +++ /dev/null @@ -1,56 +0,0 @@ -package test; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.Test; -import src.B015_7segment; -import src.InputData; -import src.Result; - -public class B015_Test { - @Test - public void 正しく判定できるかテスト() { - final InputData inputData = new InputData(Arrays.asList(1, 1, 1, 1, 0, 1, 1), Arrays - .asList(0, 1, 1, 0, 0, 0, 0)); - final Result result = B015_7segment.storeJudgementResult(inputData); - final List actual = result.getIsNumber(); - - final List expected = Arrays.asList("Yes", "No", "No"); - - assertThat(actual, is(expected)); - } - - @Test - public void 正しく表裏反転できるかテスト() { - InputData inputData = new InputData(Arrays.asList(1, 1, 1, 1, 0, 1, 1), Arrays - .asList(0, 1, 1, 0, 0, 0, 0)); - InputData insideOutData = B015_7segment.insideOut(inputData); - List actualLeft = new ArrayList<>(insideOutData.getLeftSegment()); - List actualRight = new ArrayList<>(insideOutData.getRightSegment()); - - List expectedLeft = Arrays.asList(0, 0, 0, 0, 1, 1, 0); - List expectedRight = Arrays.asList(1, 1, 0, 1, 1, 1, 1); - - assertThat(actualLeft, is(expectedLeft)); - assertThat(actualRight, is(expectedRight)); - } - - @Test - public void 正しく上下反転できるかテスト() { - InputData inputData = new InputData(Arrays.asList(0, 0, 0, 0, 1, 1, 0), Arrays - .asList(1, 1, 0, 1, 1, 1, 1)); - InputData upsideDownData = B015_7segment.upsideDown(inputData); - List actualLeft = new ArrayList<>(upsideDownData.getLeftSegment()); - List actualRight = new ArrayList<>(upsideDownData.getRightSegment()); - - List expectedLeft = Arrays.asList(0, 0, 0, 0, 1, 1, 0); - List expectedRight = Arrays.asList(1, 0, 1, 1, 1, 1, 1); - - assertThat(actualLeft, is(expectedLeft)); - assertThat(actualRight, is(expectedRight)); - } - -} diff --git a/sitou/test/B118_Test.java b/sitou/test/B118_Test.java new file mode 100644 index 0000000..aaeb922 --- /dev/null +++ b/sitou/test/B118_Test.java @@ -0,0 +1,88 @@ +package test; + +import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import org.junit.Before; +import org.junit.Test; +import src.B118; +import src.Student; + +public class B118_Test { + @Before + public void resetData() { + B118.resetStudentsList(); + } + + + @Test + public void 正しく動くかテスト() throws CloneNotSupportedException { + final InputStream is = new ByteArrayInputStream(("5\n" + + "aria 145 12\n" + + "bim 132 8\n" + + "cindy 178 4\n" + + "deen 145 3\n" + + "emma 145 3").getBytes()); + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final PrintStream ps = new PrintStream(baos); + + final B118 sut = new B118(is, ps); + sut.execute(); + + final String result = baos.toString(); + assertEquals("bim" + System.lineSeparator() + + "aria" + System.lineSeparator() + + "deen" + System.lineSeparator() + + "emma" + System.lineSeparator() + + "cindy" + System.lineSeparator(), result); + } + + @Test + public void 背の順で最小を見つけられるかテスト() throws CloneNotSupportedException { + B118.getStudentsList().add(new Student("aria", 150, 5)); + B118.getStudentsList().add(new Student("bim", 140, 6)); + B118.getStudentsList().add(new Student("cindy", 130, 4)); + + Student smallestStudent = new Student("zzzzzzzzzz", 200, 1); + String result = B118.findSmallestStudent(smallestStudent); + + assertEquals("cindy", result); + } + + @Test + public void 同じ身長がいるとき条件通り最小を見つけられるかテスト() throws CloneNotSupportedException { + B118.getStudentsList().add(new Student("aria", 150, 5)); + B118.getStudentsList().add(new Student("bim", 140, 6)); + B118.getStudentsList().add(new Student("cindy", 140, 4)); + + Student smallestStudent = new Student("zzzzzzzzzz", 200, 1); + String result = B118.findSmallestStudent(smallestStudent); + + assertEquals("bim", result); + } + + @Test + public void 同じ身長で誕生月も同じとき条件通り最小を見つけられるかテスト() throws CloneNotSupportedException { + B118.getStudentsList().add(new Student("aria", 150, 5)); + B118.getStudentsList().add(new Student("bim", 140, 6)); + B118.getStudentsList().add(new Student("cindy", 140, 8)); + B118.getStudentsList().add(new Student("deen", 140, 8)); + + Student smallestStudent = new Student("zzzzzzzzzz", 200, 1); + String result = B118.findSmallestStudent(smallestStudent); + + assertEquals("cindy", result); + } + + @Test + public void 正しく生徒を比較するかどうかテスト() { + Student student1 = new Student("aria", 150, 5); + Student student2 = new Student("bim", 150, 6); + + assertTrue(B118.isSmaller(student1, student2)); + } + +} -- GitLab From 940d130e61aa55f973315f8e79278507d263d4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Thu, 29 Aug 2024 10:40:47 +0900 Subject: [PATCH 09/12] =?UTF-8?q?B118=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B118.java | 88 +++----------------------------- sitou/src/Classroom.java | 72 ++++++++++++++++++++++++++ sitou/test/B118_Test.java | 105 +++++++++++++++++++------------------- 3 files changed, 132 insertions(+), 133 deletions(-) create mode 100644 sitou/src/Classroom.java diff --git a/sitou/src/B118.java b/sitou/src/B118.java index bcaa1ea..a1795de 100644 --- a/sitou/src/B118.java +++ b/sitou/src/B118.java @@ -30,93 +30,21 @@ public class B118 { this.out = out; } - public static void main(String[] args) throws CloneNotSupportedException { + public static void main(String[] args){ final B118 b118 = new B118(System.in, System.out); - b118.execute(); + final List result = b118.execute(); + b118.output(result); } - public void execute() throws CloneNotSupportedException { - final List result = sortHeight(); - output(result, out); + public List execute(){ + Classroom classroom = new Classroom(studentsList); + return classroom.sortStudents(); } - /** - * 生徒を背の順にしてデータを格納する. - * - * @return 格納したデータ - */ - public static List sortHeight() throws CloneNotSupportedException { - final List result = new ArrayList<>(); - - // findSmallestStudentで見つけた生徒はstudentsListから削除する - // 全員確認する、つまりstudentsListが空になるまで繰り返す - while (studentsList.size() > 0) { - // 与えられた条件で最後尾になる生徒情報を初期値にする。 - final Student smallestStudent = new Student("zzzzzzzzzz", 200, 1); - result.add(findSmallestStudent(smallestStudent)); - } - return result; - } - - /** - * 残っている生徒の中で、背の順の最も小さい生徒を探す. - * - * @param smallestStudent 問題で与えられた条件で最後尾になる生徒情報 - * @return 最小の生徒の名前 - */ - public static String findSmallestStudent(Student smallestStudent) - throws CloneNotSupportedException { - // 残っているstudentsListの中で最小の生徒名を返す - // 該当の生徒はstudentsListから削除する - int studentNum = 0; - for (int i = 0; i < studentsList.size(); i++) { - Student studentToCheck = studentsList.get(i).clone(); - - if (isSmaller(smallestStudent, studentToCheck)) { - smallestStudent = studentToCheck.clone(); - studentNum = i; - } - } - studentsList.remove(studentNum); - - return smallestStudent.getName(); - } - - /** - * 問題で与えられた条件で生徒を比較する. - * 条件 - * ・背の低い順番に前から並ばせる - * ・背の高さが同じ場合、誕生月の数字が大きい方を前にする - * ・背の高さが同じでかつ誕生月が同じ場合、名前を辞書順で並べた時、早い方の人を前にする - * - * @param smallestStudent 現在最小と思われる生徒 - * @param studentToCheck 比較対象の生徒 - * @return true/false - */ - public static boolean isSmaller(final Student smallestStudent, final Student studentToCheck) { - if (smallestStudent.getHeight() > studentToCheck.getHeight()) { - return true; - } else if (smallestStudent.getHeight() == studentToCheck.getHeight()) { - if (smallestStudent.getBirthMonth() < studentToCheck.getBirthMonth()) { - return true; - } else if (smallestStudent.getBirthMonth() == studentToCheck.getBirthMonth()) { - return smallestStudent.getName().compareTo(studentToCheck.getName()) > 0; - } - } - return false; - } - - private static void output(final List result, final PrintStream out) { + public void output(final List result) { for (int i = 0; i < result.size(); i++) { out.println(result.get(i)); } } - - public static List getStudentsList() { - return studentsList; - } - - public static void resetStudentsList() { - studentsList.clear(); - } + } diff --git a/sitou/src/Classroom.java b/sitou/src/Classroom.java new file mode 100644 index 0000000..3d03368 --- /dev/null +++ b/sitou/src/Classroom.java @@ -0,0 +1,72 @@ +package src; + +import java.util.ArrayList; +import java.util.List; + +public class Classroom { + private List studentsList; + + public Classroom(final List studentsList) { + this.studentsList = studentsList; + } + + /** + * 生徒を背の順にしてデータを格納する. + * + * @return 格納したデータ + */ + public List sortStudents() { + final List result = new ArrayList<>(); + + // findfrontStudentで見つけた生徒はstudentsListから削除していく + while (studentsList.size() > 0) { + result.add(findFrontStudent()); + } + + return result; + } + + /** + * 残っている生徒の中で、背の順の最も小さい生徒を探す. + * + * @return 最小の生徒の名前 + */ + private String findFrontStudent() { + // 与えられた条件で最後尾になる生徒情報を初期値にする。 + Student frontStudent = new Student("zzzzzzzzzz", 200, 1); + + int studentNum = 0; + for (int i = 0; i < studentsList.size(); i++) { + final Student studentToCheck = studentsList.get(i); + if (isFront(frontStudent, studentToCheck)) { + frontStudent = studentToCheck; + studentNum = i; + } + } + + studentsList.remove(studentNum); + + return frontStudent.getName(); + } + + /** + * 問題で与えられた条件で生徒を比較する. 条件 ・背の低い順番に前から並ばせる ・背の高さが同じ場合、誕生月の数字が大きい方を前にする + * ・背の高さが同じでかつ誕生月が同じ場合、名前を辞書順で並べた時、早い方の人を前にする + * + * @param frontStudent 現在最小と思われる生徒 + * @param studentToCheck 比較対象の生徒 + * @return true/false + */ + private boolean isFront(final Student frontStudent, final Student studentToCheck) { + if (frontStudent.getHeight() > studentToCheck.getHeight()) { + return true; + } else if (frontStudent.getHeight() == studentToCheck.getHeight()) { + if (frontStudent.getBirthMonth() < studentToCheck.getBirthMonth()) { + return true; + } else if (frontStudent.getBirthMonth() == studentToCheck.getBirthMonth()) { + return frontStudent.getName().compareTo(studentToCheck.getName()) > 0; + } + } + return false; + } +} diff --git a/sitou/test/B118_Test.java b/sitou/test/B118_Test.java index aaeb922..44cbeb3 100644 --- a/sitou/test/B118_Test.java +++ b/sitou/test/B118_Test.java @@ -1,88 +1,87 @@ package test; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintStream; -import org.junit.Before; +import java.util.ArrayList; +import java.util.List; import org.junit.Test; import src.B118; +import src.Classroom; import src.Student; public class B118_Test { - @Before - public void resetData() { - B118.resetStudentsList(); - } - - + @Test public void 正しく動くかテスト() throws CloneNotSupportedException { final InputStream is = new ByteArrayInputStream(("5\n" - + "aria 145 12\n" - + "bim 132 8\n" - + "cindy 178 4\n" - + "deen 145 3\n" - + "emma 145 3").getBytes()); + + "aria 145 12\n" + + "bim 132 8\n" + + "cindy 178 4\n" + + "deen 145 3\n" + + "emma 145 3").getBytes()); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final PrintStream ps = new PrintStream(baos); - final B118 sut = new B118(is, ps); - sut.execute(); + final B118 b118 = new B118(is, ps); + final List result =b118.execute(); + b118.output(result); - final String result = baos.toString(); - assertEquals("bim" + System.lineSeparator() - + "aria" + System.lineSeparator() - + "deen" + System.lineSeparator() - + "emma" + System.lineSeparator() - + "cindy" + System.lineSeparator(), result); + final String actual = baos.toString(); + assertEquals("bim" + + System.lineSeparator() + + "aria" + + System.lineSeparator() + + "deen" + + System.lineSeparator() + + "emma" + + System.lineSeparator() + + "cindy" + + System.lineSeparator(), actual); } - + @Test - public void 背の順で最小を見つけられるかテスト() throws CloneNotSupportedException { - B118.getStudentsList().add(new Student("aria", 150, 5)); - B118.getStudentsList().add(new Student("bim", 140, 6)); - B118.getStudentsList().add(new Student("cindy", 130, 4)); + public void 背の順で並べられるかテスト() throws CloneNotSupportedException { + List studentsList = new ArrayList<>(); + studentsList.add(new Student("aria", 150, 5)); + studentsList.add(new Student("bim", 140, 6)); + studentsList.add(new Student("cindy", 130, 4)); - Student smallestStudent = new Student("zzzzzzzzzz", 200, 1); - String result = B118.findSmallestStudent(smallestStudent); + Classroom classroom = new Classroom(studentsList); + final List result = classroom.sortStudents(); - assertEquals("cindy", result); + assertThat(result, is(contains("cindy", "bim", "aria"))); } - + @Test - public void 同じ身長がいるとき条件通り最小を見つけられるかテスト() throws CloneNotSupportedException { - B118.getStudentsList().add(new Student("aria", 150, 5)); - B118.getStudentsList().add(new Student("bim", 140, 6)); - B118.getStudentsList().add(new Student("cindy", 140, 4)); + public void 同じ身長がいるとき条件通り並べられるかテスト() throws CloneNotSupportedException { + List studentsList = new ArrayList<>(); + studentsList.add(new Student("aria", 150, 5)); + studentsList.add(new Student("bim", 140, 6)); + studentsList.add(new Student("cindy", 140, 4)); - Student smallestStudent = new Student("zzzzzzzzzz", 200, 1); - String result = B118.findSmallestStudent(smallestStudent); + Classroom classroom = new Classroom(studentsList); + final List result = classroom.sortStudents(); - assertEquals("bim", result); + assertThat(result, is(contains("bim", "cindy", "aria"))); } - + @Test - public void 同じ身長で誕生月も同じとき条件通り最小を見つけられるかテスト() throws CloneNotSupportedException { - B118.getStudentsList().add(new Student("aria", 150, 5)); - B118.getStudentsList().add(new Student("bim", 140, 6)); - B118.getStudentsList().add(new Student("cindy", 140, 8)); - B118.getStudentsList().add(new Student("deen", 140, 8)); + public void 同じ身長で誕生月も同じとき条件通り並べられるかテスト() throws CloneNotSupportedException { + List studentsList = new ArrayList<>(); + studentsList.add(new Student("aria", 150, 5)); + studentsList.add(new Student("bim", 140, 6)); + studentsList.add(new Student("cindy", 140, 8)); + studentsList.add(new Student("deen", 140, 8)); - Student smallestStudent = new Student("zzzzzzzzzz", 200, 1); - String result = B118.findSmallestStudent(smallestStudent); + Classroom classroom = new Classroom(studentsList); + final List result = classroom.sortStudents(); - assertEquals("cindy", result); - } - - @Test - public void 正しく生徒を比較するかどうかテスト() { - Student student1 = new Student("aria", 150, 5); - Student student2 = new Student("bim", 150, 6); - - assertTrue(B118.isSmaller(student1, student2)); + assertThat(result, is(contains("cindy", "deen", "bim", "aria"))); } } -- GitLab From 140bfee179e7119629690e9006844ebc0faf44e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Fri, 30 Aug 2024 10:52:41 +0900 Subject: [PATCH 10/12] =?UTF-8?q?paiza=E3=81=AEB032=E3=81=AE=E5=9B=9E?= =?UTF-8?q?=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B032.java | 47 ++++++++++++++++++++ sitou/src/B118.java | 50 ---------------------- sitou/src/Calculate.java | 89 ++++++++++++++++++++++++++++++++++++++ sitou/src/Classroom.java | 72 ------------------------------- sitou/src/SorobanNum.java | 48 +++++++++++++++++++++ sitou/src/Student.java | 31 -------------- sitou/test/B032_Test.java | 90 +++++++++++++++++++++++++++++++++++++++ sitou/test/B118_Test.java | 87 ------------------------------------- 8 files changed, 274 insertions(+), 240 deletions(-) create mode 100644 sitou/src/B032.java delete mode 100644 sitou/src/B118.java create mode 100644 sitou/src/Calculate.java delete mode 100644 sitou/src/Classroom.java create mode 100644 sitou/src/SorobanNum.java delete mode 100644 sitou/src/Student.java create mode 100644 sitou/test/B032_Test.java delete mode 100644 sitou/test/B118_Test.java diff --git a/sitou/src/B032.java b/sitou/src/B032.java new file mode 100644 index 0000000..fa5c66f --- /dev/null +++ b/sitou/src/B032.java @@ -0,0 +1,47 @@ +package src; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Scanner; + +public class B032 { + private static int digitsNum; + private final static String[] s1 = new String[8]; + private final static String[] s2 = new String[8]; + private PrintStream out; + + public B032(InputStream in, PrintStream out) { + final Scanner scan = new Scanner(in); + + try { + digitsNum = scan.nextInt(); + for (int i = 0; i < 8; i++) { + s1[i] = scan.next(); + } + for (int i = 0; i < 8; i++) { + s2[i] = scan.next(); + } + + } finally { + scan.close(); + } + this.out = out; + } + + public static void main(String[] args) { + final B032 b032 = new B032(System.in, System.out); + String[] result = b032.execute(); + b032.output(result); + } + + public String[] execute() { + Calculate c = new Calculate(digitsNum, s1, s2); + return c.add(); + } + + public void output(final String[] result) { + for (String row : result) { + out.println(row); + } + } +} diff --git a/sitou/src/B118.java b/sitou/src/B118.java deleted file mode 100644 index a1795de..0000000 --- a/sitou/src/B118.java +++ /dev/null @@ -1,50 +0,0 @@ -package src; - -import java.io.InputStream; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class B118 { - private static int studentsNum; - private static final List studentsList = new ArrayList<>(); - private PrintStream out; - - public B118(InputStream in, PrintStream out) { - final Scanner scan = new Scanner(in); - - try { - studentsNum = scan.nextInt(); - for (int n = 0; n < studentsNum; n++) { - final String name = scan.next(); - final int height = scan.nextInt(); - final int birthMonth = scan.nextInt(); - final Student student = new Student(name, height, birthMonth); - studentsList.add(student); - } - - } finally { - scan.close(); - } - this.out = out; - } - - public static void main(String[] args){ - final B118 b118 = new B118(System.in, System.out); - final List result = b118.execute(); - b118.output(result); - } - - public List execute(){ - Classroom classroom = new Classroom(studentsList); - return classroom.sortStudents(); - } - - public void output(final List result) { - for (int i = 0; i < result.size(); i++) { - out.println(result.get(i)); - } - } - -} diff --git a/sitou/src/Calculate.java b/sitou/src/Calculate.java new file mode 100644 index 0000000..0752868 --- /dev/null +++ b/sitou/src/Calculate.java @@ -0,0 +1,89 @@ +package src; + +public class Calculate { + private final int digitsNum; + private final String[] s1; + private final String[] s2; + + public Calculate(int digitsNum, String[] s1, String[] s2) { + this.digitsNum = digitsNum; + this.s1 = s1; + this.s2 = s2; + } + + /** + * 入力したデータの加算処理. + * + * @return 計算結果(そろばん表示) + */ + public String[] add() { + + int[] num1 = sorobanToNumber(s1); + int[] num2 = sorobanToNumber(s2); + int[] sum = new int[digitsNum]; + int carry = 0; + + // 1の位から順に加算する + // num1とnum2の同じ位の数を足して、sumの同じ位に入れる + // 繰り上げはcarryに入れる + for (int digit = digitsNum - 1; digit >= 0; digit--) { + int tempSum = num1[digit] + num2[digit] + carry; + sum[digit] = tempSum % 10; + carry = tempSum / 10; + + // 一番大きい位で繰り上がるとき、桁を拡張する + if (digit == 0 && carry != 0) { + int[] sum2 = new int[digitsNum + 1]; + sum2[0] = carry; + for (int i = 1; i < sum2.length; i++) { + sum2[i] = sum[i - 1]; + } + return numberToSoroban(sum2); + } + } + + return numberToSoroban(sum); + } + + /** + * そろばん表示を数字にする. + * + * @param soroban そろばん表示 + * @return 受け取ったそろばん表示に該当する数字 + */ + public int[] sorobanToNumber(final String[] soroban) { + int[] number = new int[soroban[0].length()]; + for (int digit = 0; digit < soroban[0].length(); digit++) { + String beads = ""; + for (int i = 0; i < 8; i++) { + beads += soroban[i].charAt(digit); + } + SorobanNum sorobanNum = SorobanNum.getByBeads(beads); + number[digit] = sorobanNum.getNum(); + } + + return number; + } + + /** + * 数字をそろばん表示にする. + * + * @param number 数字 + * @return 受け取った数字に該当するそろばん表示 + */ + public String[] numberToSoroban(final int[] number) { + String[] soroban = new String[8]; + for (int i = 0; i < 8; i++) { + soroban[i] = ""; + } + + for (int n : number) { + SorobanNum sorobanNum = SorobanNum.getByNum(n); + String beads = sorobanNum.getBeads(); + for (int i = 0; i < 8; i++) { + soroban[i] += beads.charAt(i); + } + } + return soroban; + } +} diff --git a/sitou/src/Classroom.java b/sitou/src/Classroom.java deleted file mode 100644 index 3d03368..0000000 --- a/sitou/src/Classroom.java +++ /dev/null @@ -1,72 +0,0 @@ -package src; - -import java.util.ArrayList; -import java.util.List; - -public class Classroom { - private List studentsList; - - public Classroom(final List studentsList) { - this.studentsList = studentsList; - } - - /** - * 生徒を背の順にしてデータを格納する. - * - * @return 格納したデータ - */ - public List sortStudents() { - final List result = new ArrayList<>(); - - // findfrontStudentで見つけた生徒はstudentsListから削除していく - while (studentsList.size() > 0) { - result.add(findFrontStudent()); - } - - return result; - } - - /** - * 残っている生徒の中で、背の順の最も小さい生徒を探す. - * - * @return 最小の生徒の名前 - */ - private String findFrontStudent() { - // 与えられた条件で最後尾になる生徒情報を初期値にする。 - Student frontStudent = new Student("zzzzzzzzzz", 200, 1); - - int studentNum = 0; - for (int i = 0; i < studentsList.size(); i++) { - final Student studentToCheck = studentsList.get(i); - if (isFront(frontStudent, studentToCheck)) { - frontStudent = studentToCheck; - studentNum = i; - } - } - - studentsList.remove(studentNum); - - return frontStudent.getName(); - } - - /** - * 問題で与えられた条件で生徒を比較する. 条件 ・背の低い順番に前から並ばせる ・背の高さが同じ場合、誕生月の数字が大きい方を前にする - * ・背の高さが同じでかつ誕生月が同じ場合、名前を辞書順で並べた時、早い方の人を前にする - * - * @param frontStudent 現在最小と思われる生徒 - * @param studentToCheck 比較対象の生徒 - * @return true/false - */ - private boolean isFront(final Student frontStudent, final Student studentToCheck) { - if (frontStudent.getHeight() > studentToCheck.getHeight()) { - return true; - } else if (frontStudent.getHeight() == studentToCheck.getHeight()) { - if (frontStudent.getBirthMonth() < studentToCheck.getBirthMonth()) { - return true; - } else if (frontStudent.getBirthMonth() == studentToCheck.getBirthMonth()) { - return frontStudent.getName().compareTo(studentToCheck.getName()) > 0; - } - } - return false; - } -} diff --git a/sitou/src/SorobanNum.java b/sitou/src/SorobanNum.java new file mode 100644 index 0000000..a1ace93 --- /dev/null +++ b/sitou/src/SorobanNum.java @@ -0,0 +1,48 @@ +package src; + +public enum SorobanNum { + ZERO(0, "*|=|****"), + ONE(1, "*|=*|***"), + TWO(2, "*|=**|**"), + THREE(3, "*|=***|*"), + FOUR(4, "*|=****|"), + FIVE(5, "|*=|****"), + SIX(6, "|*=*|***"), + SEVEN(7, "|*=**|**"), + EIGHT(8, "|*=***|*"), + NINE(9, "|*=****|"); + + private final int num; + private final String beads; + + private SorobanNum(int num, String beads) { + this.num = num; + this.beads = beads; + } + + public int getNum() { + return num; + } + + public String getBeads() { + return beads; + } + + public static SorobanNum getByNum(final int num) { + for (SorobanNum sorobanNum : SorobanNum.values()) { + if (sorobanNum.getNum() == num) { + return sorobanNum; + } + } + return null; + } + + public static SorobanNum getByBeads(final String beads) { + for (SorobanNum sorobanNum : SorobanNum.values()) { + if (sorobanNum.getBeads().equals(beads)) { + return sorobanNum; + } + } + return null; + } +} diff --git a/sitou/src/Student.java b/sitou/src/Student.java deleted file mode 100644 index 2a3a09d..0000000 --- a/sitou/src/Student.java +++ /dev/null @@ -1,31 +0,0 @@ -package src; - -public class Student implements Cloneable { - private final String name; - private final int height; - private final int birthMonth; - - public Student(final String name, final int height, final int birthMonth) { - this.name = name; - this.height = height; - this.birthMonth = birthMonth; - } - - public String getName() { - return name; - } - - public int getHeight() { - return height; - } - - public int getBirthMonth() { - return birthMonth; - } - - @Override - public Student clone() throws CloneNotSupportedException { - Student clone = (Student) super.clone(); - return clone; - } -} diff --git a/sitou/test/B032_Test.java b/sitou/test/B032_Test.java new file mode 100644 index 0000000..109be4e --- /dev/null +++ b/sitou/test/B032_Test.java @@ -0,0 +1,90 @@ +package test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import org.junit.Test; +import src.B032; +import src.Calculate; + +public class B032_Test { + @Test + public void 正しく動くかテスト() throws CloneNotSupportedException { + final InputStream is = new ByteArrayInputStream(("6\n" + +"**|***\n" + +"||*|||\n" + +"======\n" + +"*|*|*|\n" + +"****|*\n" + +"******\n" + +"**|***\n" + +"|*****\n" + +"**|***\n" + +"||*|||\n" + +"======\n" + +"||*|*|\n" + +"****|*\n" + +"******\n" + +"**|***\n" + +"******").getBytes()); + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final PrintStream ps = new PrintStream(baos); + + final B032 b032 = new B032(is, ps); + final String[] result = b032.execute(); + b032.output(result); + + final String actual = baos.toString(); + assertEquals("**|***" + System.lineSeparator() + + "||*|||" + System.lineSeparator() + + "======" + System.lineSeparator() + + "***|*|" + System.lineSeparator() + + "*||***" + System.lineSeparator() + + "****|*" + System.lineSeparator() + + "******" + System.lineSeparator() + + "|*****" + System.lineSeparator(), actual); + } + + @Test + public void 一番大きい位で繰り上がるとき999足す999(){ + int digitsNum = 3; + String[] s1 = new String[] {"|||" ,"***", "===", "***", "***", "***", "***", "|||"}; + String[] s2 = new String[] {"|||" ,"***", "===", "***", "***", "***", "***", "|||"}; + + Calculate c = new Calculate(digitsNum, s1, s2); + String[] actual = c.add(); + + assertThat(actual, arrayContaining("*|||" + , "|***" + , "====" + , "****" + , "|***" + , "****" + , "***|" + , "*||*")); + } + + @Test + public void 一番大きい位で繰り上がらないとき444足す444(){ + int digitsNum = 3; + String[] s1 = new String[] {"***", "|||", "===", "***", "***", "***", "***", "|||"}; + String[] s2 = new String[] {"***", "|||", "===", "***", "***", "***", "***", "|||"}; + + Calculate c = new Calculate(digitsNum, s1, s2); + String[] actual = c.add(); + + assertThat(actual, arrayContaining("|||" + , "***" + , "===" + , "***" + , "***" + , "***" + , "|||" + , "***")); + } + +} diff --git a/sitou/test/B118_Test.java b/sitou/test/B118_Test.java deleted file mode 100644 index 44cbeb3..0000000 --- a/sitou/test/B118_Test.java +++ /dev/null @@ -1,87 +0,0 @@ -package test; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.List; -import org.junit.Test; -import src.B118; -import src.Classroom; -import src.Student; - -public class B118_Test { - - @Test - public void 正しく動くかテスト() throws CloneNotSupportedException { - final InputStream is = new ByteArrayInputStream(("5\n" - + "aria 145 12\n" - + "bim 132 8\n" - + "cindy 178 4\n" - + "deen 145 3\n" - + "emma 145 3").getBytes()); - - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final PrintStream ps = new PrintStream(baos); - - final B118 b118 = new B118(is, ps); - final List result =b118.execute(); - b118.output(result); - - final String actual = baos.toString(); - assertEquals("bim" - + System.lineSeparator() - + "aria" - + System.lineSeparator() - + "deen" - + System.lineSeparator() - + "emma" - + System.lineSeparator() - + "cindy" - + System.lineSeparator(), actual); - } - - @Test - public void 背の順で並べられるかテスト() throws CloneNotSupportedException { - List studentsList = new ArrayList<>(); - studentsList.add(new Student("aria", 150, 5)); - studentsList.add(new Student("bim", 140, 6)); - studentsList.add(new Student("cindy", 130, 4)); - - Classroom classroom = new Classroom(studentsList); - final List result = classroom.sortStudents(); - - assertThat(result, is(contains("cindy", "bim", "aria"))); - } - - @Test - public void 同じ身長がいるとき条件通り並べられるかテスト() throws CloneNotSupportedException { - List studentsList = new ArrayList<>(); - studentsList.add(new Student("aria", 150, 5)); - studentsList.add(new Student("bim", 140, 6)); - studentsList.add(new Student("cindy", 140, 4)); - - Classroom classroom = new Classroom(studentsList); - final List result = classroom.sortStudents(); - - assertThat(result, is(contains("bim", "cindy", "aria"))); - } - - @Test - public void 同じ身長で誕生月も同じとき条件通り並べられるかテスト() throws CloneNotSupportedException { - List studentsList = new ArrayList<>(); - studentsList.add(new Student("aria", 150, 5)); - studentsList.add(new Student("bim", 140, 6)); - studentsList.add(new Student("cindy", 140, 8)); - studentsList.add(new Student("deen", 140, 8)); - - Classroom classroom = new Classroom(studentsList); - final List result = classroom.sortStudents(); - - assertThat(result, is(contains("cindy", "deen", "bim", "aria"))); - } - -} -- GitLab From dfff13f2e275260f83328ae20b8ad6601d063c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Mon, 2 Sep 2024 10:26:54 +0900 Subject: [PATCH 11/12] =?UTF-8?q?B032=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B032.java | 16 +++++++------- sitou/src/Calculate.java | 46 +++++++++++++++++++-------------------- sitou/test/B032_Test.java | 6 ++--- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/sitou/src/B032.java b/sitou/src/B032.java index fa5c66f..070002b 100644 --- a/sitou/src/B032.java +++ b/sitou/src/B032.java @@ -5,21 +5,21 @@ import java.io.PrintStream; import java.util.Scanner; public class B032 { - private static int digitsNum; - private final static String[] s1 = new String[8]; - private final static String[] s2 = new String[8]; + private static int sorobanLength; + private final static String[] addendOfSoroban1 = new String[8]; + private final static String[] addendOfSoroban2 = new String[8]; private PrintStream out; public B032(InputStream in, PrintStream out) { final Scanner scan = new Scanner(in); try { - digitsNum = scan.nextInt(); + sorobanLength = scan.nextInt(); for (int i = 0; i < 8; i++) { - s1[i] = scan.next(); + addendOfSoroban1[i] = scan.next(); } for (int i = 0; i < 8; i++) { - s2[i] = scan.next(); + addendOfSoroban2[i] = scan.next(); } } finally { @@ -35,8 +35,8 @@ public class B032 { } public String[] execute() { - Calculate c = new Calculate(digitsNum, s1, s2); - return c.add(); + Calculate calculate = new Calculate(sorobanLength, addendOfSoroban1, addendOfSoroban2); + return calculate.add(); } public void output(final String[] result) { diff --git a/sitou/src/Calculate.java b/sitou/src/Calculate.java index 0752868..1206345 100644 --- a/sitou/src/Calculate.java +++ b/sitou/src/Calculate.java @@ -1,14 +1,17 @@ package src; +import java.util.ArrayList; +import java.util.List; + public class Calculate { - private final int digitsNum; - private final String[] s1; - private final String[] s2; + private final int sorobanLength; + private final String[] addendOfSoroban1; + private final String[] addendOfSoroban2; - public Calculate(int digitsNum, String[] s1, String[] s2) { - this.digitsNum = digitsNum; - this.s1 = s1; - this.s2 = s2; + public Calculate(int sorobanLength, String[] addendOfSoroban1, String[] addendOfSoroban2) { + this.sorobanLength = sorobanLength; + this.addendOfSoroban1 = addendOfSoroban1; + this.addendOfSoroban2 = addendOfSoroban2; } /** @@ -18,31 +21,26 @@ public class Calculate { */ public String[] add() { - int[] num1 = sorobanToNumber(s1); - int[] num2 = sorobanToNumber(s2); - int[] sum = new int[digitsNum]; + int[] addend1 = sorobanToNumber(addendOfSoroban1); + int[] addend2 = sorobanToNumber(addendOfSoroban2); + List sumResult = new ArrayList<>(); int carry = 0; // 1の位から順に加算する - // num1とnum2の同じ位の数を足して、sumの同じ位に入れる + // addend1とaddend2の同じ位の数を足して、sumの同じ位に入れる // 繰り上げはcarryに入れる - for (int digit = digitsNum - 1; digit >= 0; digit--) { - int tempSum = num1[digit] + num2[digit] + carry; - sum[digit] = tempSum % 10; + for (int digit = sorobanLength - 1; digit >= 0; digit--) { + int tempSum = addend1[digit] + addend2[digit] + carry; + sumResult.add(0, tempSum % 10); carry = tempSum / 10; - // 一番大きい位で繰り上がるとき、桁を拡張する + // 一番大きい位で繰り上がるとき if (digit == 0 && carry != 0) { - int[] sum2 = new int[digitsNum + 1]; - sum2[0] = carry; - for (int i = 1; i < sum2.length; i++) { - sum2[i] = sum[i - 1]; - } - return numberToSoroban(sum2); + sumResult.add(0, carry); } } - return numberToSoroban(sum); + return numberToSoroban(sumResult); } /** @@ -71,13 +69,13 @@ public class Calculate { * @param number 数字 * @return 受け取った数字に該当するそろばん表示 */ - public String[] numberToSoroban(final int[] number) { + public String[] numberToSoroban(final List number) { String[] soroban = new String[8]; for (int i = 0; i < 8; i++) { soroban[i] = ""; } - for (int n : number) { + for (Integer n : number) { SorobanNum sorobanNum = SorobanNum.getByNum(n); String beads = sorobanNum.getBeads(); for (int i = 0; i < 8; i++) { diff --git a/sitou/test/B032_Test.java b/sitou/test/B032_Test.java index 109be4e..91a02c5 100644 --- a/sitou/test/B032_Test.java +++ b/sitou/test/B032_Test.java @@ -12,7 +12,7 @@ import src.Calculate; public class B032_Test { @Test - public void 正しく動くかテスト() throws CloneNotSupportedException { + public void 入力されたそろばん表示の文字を計算処理して正しい結果をそろばん表示で出力できる() throws CloneNotSupportedException { final InputStream is = new ByteArrayInputStream(("6\n" +"**|***\n" +"||*|||\n" @@ -50,7 +50,7 @@ public class B032_Test { } @Test - public void 一番大きい位で繰り上がるとき999足す999(){ + public void executeメソッドで一番大きい位が繰り上がる計算した結果を正しくそろばん表示で返す(){ int digitsNum = 3; String[] s1 = new String[] {"|||" ,"***", "===", "***", "***", "***", "***", "|||"}; String[] s2 = new String[] {"|||" ,"***", "===", "***", "***", "***", "***", "|||"}; @@ -69,7 +69,7 @@ public class B032_Test { } @Test - public void 一番大きい位で繰り上がらないとき444足す444(){ + public void executeメソッドで一番大きい位が繰り上がらない計算した結果を正しくそろばん表示で返す(){ int digitsNum = 3; String[] s1 = new String[] {"***", "|||", "===", "***", "***", "***", "***", "|||"}; String[] s2 = new String[] {"***", "|||", "===", "***", "***", "***", "***", "|||"}; -- GitLab From 63f6f06e7fca8881688cca3a044ff3fef231c229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E8=97=A4=20=E8=81=96=E5=A4=9C?= Date: Mon, 2 Sep 2024 11:34:25 +0900 Subject: [PATCH 12/12] =?UTF-8?q?=E3=81=9D=E3=82=8D=E3=81=B0=E3=82=93?= =?UTF-8?q?=E3=81=AE=E7=B8=A6=E3=81=AE=E9=95=B7=E3=81=95=E3=82=92=E5=AE=9A?= =?UTF-8?q?=E6=95=B0=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sitou/src/B032.java | 9 +++++---- sitou/src/Calculate.java | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/sitou/src/B032.java b/sitou/src/B032.java index 070002b..9e077af 100644 --- a/sitou/src/B032.java +++ b/sitou/src/B032.java @@ -5,9 +5,10 @@ import java.io.PrintStream; import java.util.Scanner; public class B032 { + private final static int SOROBANHEIGHT = 8; private static int sorobanLength; - private final static String[] addendOfSoroban1 = new String[8]; - private final static String[] addendOfSoroban2 = new String[8]; + private final static String[] addendOfSoroban1 = new String[SOROBANHEIGHT]; + private final static String[] addendOfSoroban2 = new String[SOROBANHEIGHT]; private PrintStream out; public B032(InputStream in, PrintStream out) { @@ -15,10 +16,10 @@ public class B032 { try { sorobanLength = scan.nextInt(); - for (int i = 0; i < 8; i++) { + for (int i = 0; i < SOROBANHEIGHT; i++) { addendOfSoroban1[i] = scan.next(); } - for (int i = 0; i < 8; i++) { + for (int i = 0; i < SOROBANHEIGHT; i++) { addendOfSoroban2[i] = scan.next(); } diff --git a/sitou/src/Calculate.java b/sitou/src/Calculate.java index 1206345..a015e5e 100644 --- a/sitou/src/Calculate.java +++ b/sitou/src/Calculate.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; public class Calculate { + private final int SOROBANHEIGHT = 8; private final int sorobanLength; private final String[] addendOfSoroban1; private final String[] addendOfSoroban2; @@ -53,7 +54,7 @@ public class Calculate { int[] number = new int[soroban[0].length()]; for (int digit = 0; digit < soroban[0].length(); digit++) { String beads = ""; - for (int i = 0; i < 8; i++) { + for (int i = 0; i < SOROBANHEIGHT; i++) { beads += soroban[i].charAt(digit); } SorobanNum sorobanNum = SorobanNum.getByBeads(beads); @@ -70,15 +71,15 @@ public class Calculate { * @return 受け取った数字に該当するそろばん表示 */ public String[] numberToSoroban(final List number) { - String[] soroban = new String[8]; - for (int i = 0; i < 8; i++) { + String[] soroban = new String[SOROBANHEIGHT]; + for (int i = 0; i < SOROBANHEIGHT; i++) { soroban[i] = ""; } for (Integer n : number) { SorobanNum sorobanNum = SorobanNum.getByNum(n); String beads = sorobanNum.getBeads(); - for (int i = 0; i < 8; i++) { + for (int i = 0; i < SOROBANHEIGHT; i++) { soroban[i] += beads.charAt(i); } } -- GitLab