From dafff9e6e6f9c56537ab403cdb18256b06f2f691 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Fri, 4 Jul 2025 16:44:21 +0900 Subject: [PATCH 01/11] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB138?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 190 +++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 kkanazawa/src/SpiralDecoder.java diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java new file mode 100644 index 0000000..8ecb5e8 --- /dev/null +++ b/kkanazawa/src/SpiralDecoder.java @@ -0,0 +1,190 @@ +package paiza.src; + +import java.lang.reflect.Array; +import java.util.Scanner; + +/** + * 受け取った二次元の文字リストをうずまき型で解読する + * + * @author 金澤継心 + */ +public class SpiralDecoder { + + public static void main(final String[] args) { + Scanner scanner = new Scanner(System.in); + final int lineAmount = scanner.nextInt(); // 入力値の行の数 + final int columnAmount = scanner.nextInt(); // 入力値の列の数; + final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス + final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + + // もととなる二次元の文字リストのを入力から受け取る + final String[][] cipher = inputCipher(scanner, lineAmount, columnAmount); + scanner.close(); + + // 入力値の二次元の文字リストの周りをNULLで埋める + final String[][] paddedCipher = nullPaddingCipher(cipher); + // 復号処理 + final String selectedDecode = decodingCipher(lineAmount, columnAmount, startIndex, endIndex, + paddedCipher); + System.out.println(selectedDecode); + } + + /** + * もととなる文字列を入力から受け取る + * + * @param scanner 入力を受け取るスキャナー + * @param lineAmount 文字リストの行数 + * @param columnAmount 文字リストの列数 + */ + public static String[][] inputCipher(final Scanner scanner, final int lineAmount, final int columnAmount) { + String[][] cipher = new String[lineAmount][columnAmount]; + for (int i = 0; i < lineAmount; i++) { + String inputLine = scanner.next(); + cipher[i] = inputLine.split(""); + } + return cipher; + } + + /** + * 文字リストの周りをNULLで埋める + * + * @param originCipher もととなる文字リスト + */ + public static String[][] nullPaddingCipher(final String[][] originCipher) { + final int lineAmount = Array.getLength(originCipher); + final int columnAmount = Array.getLength(originCipher[0]); + String[][] paddedCipher = new String[lineAmount + 2][columnAmount + 2]; + + // 元の文字リストをNULLしかない文字リストの中央にコピーする + for (int i = 0; i < lineAmount; i++) { + System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, columnAmount); + } + return paddedCipher; + } + + /** + * 周りをNULLで埋めた文字リストの解読 NULLにぶつかったら方向転換するイメージでうずまき状に要素を移動する + * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える + * + * @param lineAmount 文字列リストの行数 + * @param columnAmount 文字列リストの列数 + * @param startIndex 解読後読み始める文字の順番 + * @param endIndex 解読後読み終える文字の順番 + */ + public static String decodingCipher(final int lineAmount, final int columnAmount, final int startIndex, + final int endIndex, final String[][] cipher) { + // 現在いる要素を示すカーソルとして使うクラス + DecodeCursor cursor = new DecodeCursor(); + // NULLでかこっているため、始まりのインデックスは行、列ともに1から + cursor.setLineIndex(1); + cursor.setColumnIndex(1); + // 移動する方向を表すインスタンス変数 + cursor.setDirection(0); + // 現在移し終えた文字数のカウンタ + int charCounter = 0; + // この配列に文字移して最終的には復号した順番に文字が並ぶ + String[] decordResult = new String[lineAmount * columnAmount]; + + while (charCounter < lineAmount * columnAmount) { + // NULLの要素に移動した際、カーソルの位置を戻す処理 + cursor.modifying(charCounter); + // NULLのぶつかるまで一方向に要素を読み取り続ける + while (cipher[cursor.getLineIndex()][cursor.getColumnIndex()] != null) { + decordResult[charCounter] = cipher[cursor.getLineIndex()][cursor.getColumnIndex()]; + cipher[cursor.getLineIndex()][cursor.getColumnIndex()] = null; + charCounter++; + // カーソルを現在進むべき方向に移動させる + cursor.shifting(); + } + // 方向転換の処理 + cursor.changeDirection(); + } + return String.join("", decordResult).substring(startIndex - 1, endIndex); + } + +} + + +/** + * 現在読み取っている要素を示すカーソルとして使うクラス + * + */ +class DecodeCursor { + private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している + private int lineIndex;// 現在の行の位置 + private int columnIndex;// 現在の列の位置 + + public int getDirection() { + return direction; + } + + public void setDirection(int direction) { + this.direction = direction; + } + + public void changeDirection() { + this.direction++; + } + + public int getLineIndex() { + return lineIndex; + } + + public void setLineIndex(int lineIndex) { + this.lineIndex = lineIndex; + } + + public int getColumnIndex() { + return columnIndex; + } + + public void setColumnIndex(int columnIndex) { + this.columnIndex = columnIndex; + } + + // NULLの要素に移動した際、カーソルの位置を戻す処理 + // 4つの方向はdirectionを4で割った剰余として区別している + // 0:右 1:下 2:左 3:上 + public void modifying(final int charCounter) { + switch (direction % 4) { + case 0: + if (charCounter != 0) { + this.columnIndex++; + this.lineIndex++; + } + break; + case 1: + this.columnIndex--; + this.lineIndex++; + break; + case 2: + this.columnIndex--; + this.lineIndex--; + break; + case 3: + this.columnIndex++; + this.lineIndex--; + break; + } + } + + // カーソルを現在進むべき方向に移動させる + // 4つの方向はdirectionを4で割った剰余として区別している + // 0:右 1:下 2:左 3:上 + public void shifting() { + switch (this.direction % 4) { + case 0: + this.columnIndex++; + break; + case 1: + this.lineIndex++; + break; + case 2: + this.columnIndex--; + break; + case 3: + this.lineIndex--; + break; + } + } +} -- GitLab From a9235b5fba8dc52b4e5898f3b0a88d08a4ef01d8 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 7 Jul 2025 16:04:29 +0900 Subject: [PATCH 02/11] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB156?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 181 ++++++++++++++++++------------- 1 file changed, 105 insertions(+), 76 deletions(-) diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java index 8ecb5e8..be0dc1b 100644 --- a/kkanazawa/src/SpiralDecoder.java +++ b/kkanazawa/src/SpiralDecoder.java @@ -1,6 +1,7 @@ package paiza.src; import java.lang.reflect.Array; +import java.util.Arrays; import java.util.Scanner; /** @@ -12,86 +13,104 @@ public class SpiralDecoder { public static void main(final String[] args) { Scanner scanner = new Scanner(System.in); - final int lineAmount = scanner.nextInt(); // 入力値の行の数 - final int columnAmount = scanner.nextInt(); // 入力値の列の数; - final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス - final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + final int ROW_COUNT = scanner.nextInt(); // 入力値の行の数 + final int COLUMN_COUNT = scanner.nextInt(); // 入力値の列の数; + final int START_INDEX = scanner.nextInt(); // 読み始まる文字のインデックス + final int END_INDEX = scanner.nextInt(); // 読み終える文字のインデックス + final String BARRIR_CHAR = "@"; // もととなる二次元の文字リストのを入力から受け取る - final String[][] cipher = inputCipher(scanner, lineAmount, columnAmount); + final String[][] CIPHER = inputCipher(scanner, ROW_COUNT, COLUMN_COUNT); scanner.close(); // 入力値の二次元の文字リストの周りをNULLで埋める - final String[][] paddedCipher = nullPaddingCipher(cipher); - // 復号処理 - final String selectedDecode = decodingCipher(lineAmount, columnAmount, startIndex, endIndex, - paddedCipher); + final String[][] PADDED_CIPHER = paddingCipher(CIPHER, BARRIR_CHAR); + + // 解読処理 + final String selectedDecode = decodingCipher(ROW_COUNT, COLUMN_COUNT, START_INDEX, END_INDEX, + PADDED_CIPHER, BARRIR_CHAR); System.out.println(selectedDecode); } /** * もととなる文字列を入力から受け取る * - * @param scanner 入力を受け取るスキャナー - * @param lineAmount 文字リストの行数 - * @param columnAmount 文字リストの列数 + * @param SCANNER 入力を受け取るスキャナー + * @param ROW_COUNT 文字リストの行数 + * @param COLUMN_COUNT 文字リストの列数 */ - public static String[][] inputCipher(final Scanner scanner, final int lineAmount, final int columnAmount) { - String[][] cipher = new String[lineAmount][columnAmount]; - for (int i = 0; i < lineAmount; i++) { - String inputLine = scanner.next(); - cipher[i] = inputLine.split(""); + public static String[][] inputCipher(final Scanner SCANNER, final int ROW_COUNT, + final int COLUMN_COUNT) { + String[][] cipher = new String[ROW_COUNT][COLUMN_COUNT]; + for (int i = 0; i < ROW_COUNT; i++) { + String inputRow = SCANNER.next(); + cipher[i] = inputRow.split(""); } return cipher; } /** - * 文字リストの周りをNULLで埋める + * 文字リストの周りを@で埋める * - * @param originCipher もととなる文字リスト + * @param ORIGIN_CIPHER もととなる文字リスト */ - public static String[][] nullPaddingCipher(final String[][] originCipher) { - final int lineAmount = Array.getLength(originCipher); - final int columnAmount = Array.getLength(originCipher[0]); - String[][] paddedCipher = new String[lineAmount + 2][columnAmount + 2]; - - // 元の文字リストをNULLしかない文字リストの中央にコピーする - for (int i = 0; i < lineAmount; i++) { - System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, columnAmount); + public static String[][] paddingCipher(final String[][] ORIGIN_CIPHER, final String BARRIR_CHAR) { + final int ROW_COUNT = Array.getLength(ORIGIN_CIPHER); + final int COLUMN_COUNT = Array.getLength(ORIGIN_CIPHER[0]); + String[][] paddedCipher = new String[ROW_COUNT + 2][COLUMN_COUNT + 2]; + + for (String[] o : paddedCipher) { + Arrays.fill(o, BARRIR_CHAR); + } + + // 元の文字リストを"@"しかない文字リストの中央にコピーする + for (int i = 0; i < ROW_COUNT; i++) { + System.arraycopy(ORIGIN_CIPHER[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); } return paddedCipher; } /** - * 周りをNULLで埋めた文字リストの解読 NULLにぶつかったら方向転換するイメージでうずまき状に要素を移動する + * 周りをNULLで埋めた文字リストの解読 @にぶつかったら方向転換するイメージでうずまき状に要素を移動する * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える * - * @param lineAmount 文字列リストの行数 - * @param columnAmount 文字列リストの列数 - * @param startIndex 解読後読み始める文字の順番 - * @param endIndex 解読後読み終える文字の順番 + * @param ROW_COUNT 文字列リストの行数 + * @param COLUMN_COUNT 文字列リストの列数 + * @param START_INDEX 解読後読み始める文字の順番 + * @param END_INDEX 解読後読み終える文字の順番 */ - public static String decodingCipher(final int lineAmount, final int columnAmount, final int startIndex, - final int endIndex, final String[][] cipher) { + public static String decodingCipher(final int ROW_COUNT, final int COLUMN_COUNT, + final int START_INDEX, + final int END_INDEX, final String[][] CIPHER, final String BARRIR_CHAR) { // 現在いる要素を示すカーソルとして使うクラス DecodeCursor cursor = new DecodeCursor(); // NULLでかこっているため、始まりのインデックスは行、列ともに1から - cursor.setLineIndex(1); + cursor.setRowIndex(1); cursor.setColumnIndex(1); // 移動する方向を表すインスタンス変数 cursor.setDirection(0); // 現在移し終えた文字数のカウンタ int charCounter = 0; // この配列に文字移して最終的には復号した順番に文字が並ぶ - String[] decordResult = new String[lineAmount * columnAmount]; - - while (charCounter < lineAmount * columnAmount) { - // NULLの要素に移動した際、カーソルの位置を戻す処理 - cursor.modifying(charCounter); - // NULLのぶつかるまで一方向に要素を読み取り続ける - while (cipher[cursor.getLineIndex()][cursor.getColumnIndex()] != null) { - decordResult[charCounter] = cipher[cursor.getLineIndex()][cursor.getColumnIndex()]; - cipher[cursor.getLineIndex()][cursor.getColumnIndex()] = null; + String[] decodedResult = new String[ROW_COUNT * COLUMN_COUNT]; + + // 初期位置から初めて@にぶつかるまでの解読処理 + while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { + decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; + CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + charCounter++; + cursor.shifting(); + } + cursor.changeDirection(); + + // 初めて@にぶつかってから解読し終わるまでの処理 + while (charCounter < ROW_COUNT * COLUMN_COUNT) { + // @の要素に移動した際、カーソルの位置を戻す処理 + cursor.modifying(); + // @のぶつかるまで一方向に要素を読み取り続ける + while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { + decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; + CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; charCounter++; // カーソルを現在進むべき方向に移動させる cursor.shifting(); @@ -99,7 +118,7 @@ public class SpiralDecoder { // 方向転換の処理 cursor.changeDirection(); } - return String.join("", decordResult).substring(startIndex - 1, endIndex); + return String.join("", decodedResult).substring(START_INDEX - 1, END_INDEX); } } @@ -111,59 +130,63 @@ public class SpiralDecoder { */ class DecodeCursor { private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している - private int lineIndex;// 現在の行の位置 + private int rowIndex;// 現在の行の位置 private int columnIndex;// 現在の列の位置 public int getDirection() { return direction; } - public void setDirection(int direction) { - this.direction = direction; + public void setDirection(final int DIRECTION) { + this.direction = DIRECTION; } public void changeDirection() { this.direction++; } - public int getLineIndex() { - return lineIndex; + public int getRowIndex() { + return rowIndex; } - public void setLineIndex(int lineIndex) { - this.lineIndex = lineIndex; + public void setRowIndex(final int ROW_INDEX) { + this.rowIndex = ROW_INDEX; } public int getColumnIndex() { return columnIndex; } - public void setColumnIndex(int columnIndex) { - this.columnIndex = columnIndex; + public void setColumnIndex(final int COLUMN_INDEX) { + this.columnIndex = COLUMN_INDEX; } - // NULLの要素に移動した際、カーソルの位置を戻す処理 - // 4つの方向はdirectionを4で割った剰余として区別している + // NULL(要検討)の要素に移動した際、カーソルの位置を戻す処理 + // 4つの方向はdirectionを4で割った剰余として区別する // 0:右 1:下 2:左 3:上 - public void modifying(final int charCounter) { - switch (direction % 4) { - case 0: - if (charCounter != 0) { - this.columnIndex++; - this.lineIndex++; - } + public void modifying() { + final int DIRECTION_NUM = this.direction % 4; + final int RIGHT_NUM = 0; + final int DOWN_NUM = 1; + final int LEFT_NUM = 2; + final int UP_NUM = 3; + + switch (DIRECTION_NUM) { + case RIGHT_NUM: + this.columnIndex++; + this.rowIndex++; break; - case 1: + case DOWN_NUM: this.columnIndex--; - this.lineIndex++; + this.rowIndex++; break; - case 2: + case LEFT_NUM: this.columnIndex--; - this.lineIndex--; + this.rowIndex--; break; - case 3: + case UP_NUM: this.columnIndex++; - this.lineIndex--; + this.rowIndex--; break; } } @@ -172,18 +195,24 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別している // 0:右 1:下 2:左 3:上 public void shifting() { - switch (this.direction % 4) { - case 0: + final int DIRECTION_NUM = this.direction % 4; + final int RIGHT_NUM = 0; + final int DOWN_NUM = 1; + final int LEFT_NUM = 2; + final int UP_NUM = 3; + + switch (DIRECTION_NUM) { + case RIGHT_NUM: this.columnIndex++; break; - case 1: - this.lineIndex++; + case DOWN_NUM: + this.rowIndex++; break; - case 2: + case LEFT_NUM: this.columnIndex--; break; - case 3: - this.lineIndex--; + case UP_NUM: + this.rowIndex--; break; } } -- GitLab From cad12ab177baec88577281987f99500f6fe0c6b4 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 7 Jul 2025 17:44:16 +0900 Subject: [PATCH 03/11] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB156?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 119 +++++++++++++++---------------- 1 file changed, 58 insertions(+), 61 deletions(-) diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java index be0dc1b..45c2046 100644 --- a/kkanazawa/src/SpiralDecoder.java +++ b/kkanazawa/src/SpiralDecoder.java @@ -13,37 +13,37 @@ public class SpiralDecoder { public static void main(final String[] args) { Scanner scanner = new Scanner(System.in); - final int ROW_COUNT = scanner.nextInt(); // 入力値の行の数 - final int COLUMN_COUNT = scanner.nextInt(); // 入力値の列の数; - final int START_INDEX = scanner.nextInt(); // 読み始まる文字のインデックス - final int END_INDEX = scanner.nextInt(); // 読み終える文字のインデックス - final String BARRIR_CHAR = "@"; + final int rowCount = scanner.nextInt(); // 入力値の行の数 + final int columnCount = scanner.nextInt(); // 入力値の列の数; + final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス + final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + final String barrirchar = "@"; // もととなる二次元の文字リストのを入力から受け取る - final String[][] CIPHER = inputCipher(scanner, ROW_COUNT, COLUMN_COUNT); + final String[][] cipher = inputCipher(scanner, rowCount, columnCount); scanner.close(); // 入力値の二次元の文字リストの周りをNULLで埋める - final String[][] PADDED_CIPHER = paddingCipher(CIPHER, BARRIR_CHAR); + final String[][] paddedCipher = paddingCipher(cipher, barrirchar); // 解読処理 - final String selectedDecode = decodingCipher(ROW_COUNT, COLUMN_COUNT, START_INDEX, END_INDEX, - PADDED_CIPHER, BARRIR_CHAR); + final String selectedDecode = decodingCipher(rowCount, columnCount, startIndex, endIndex, + paddedCipher, barrirchar); System.out.println(selectedDecode); } /** * もととなる文字列を入力から受け取る * - * @param SCANNER 入力を受け取るスキャナー - * @param ROW_COUNT 文字リストの行数 - * @param COLUMN_COUNT 文字リストの列数 + * @param scanner 入力を受け取るスキャナー + * @param rowCount 文字リストの行数 + * @param columnCount 文字リストの列数 */ - public static String[][] inputCipher(final Scanner SCANNER, final int ROW_COUNT, - final int COLUMN_COUNT) { - String[][] cipher = new String[ROW_COUNT][COLUMN_COUNT]; - for (int i = 0; i < ROW_COUNT; i++) { - String inputRow = SCANNER.next(); + public static String[][] inputCipher(final Scanner scanner, final int rowCount, + final int columnCount) { + String[][] cipher = new String[rowCount][columnCount]; + for (int i = 0; i < rowCount; i++) { + String inputRow = scanner.next(); cipher[i] = inputRow.split(""); } return cipher; @@ -52,20 +52,20 @@ public class SpiralDecoder { /** * 文字リストの周りを@で埋める * - * @param ORIGIN_CIPHER もととなる文字リスト + * @param originCipher もととなる文字リスト */ - public static String[][] paddingCipher(final String[][] ORIGIN_CIPHER, final String BARRIR_CHAR) { - final int ROW_COUNT = Array.getLength(ORIGIN_CIPHER); - final int COLUMN_COUNT = Array.getLength(ORIGIN_CIPHER[0]); + public static String[][] paddingCipher(final String[][] originCipher, final String barrirchar) { + final int ROW_COUNT = Array.getLength(originCipher); + final int COLUMN_COUNT = Array.getLength(originCipher[0]); String[][] paddedCipher = new String[ROW_COUNT + 2][COLUMN_COUNT + 2]; for (String[] o : paddedCipher) { - Arrays.fill(o, BARRIR_CHAR); + Arrays.fill(o, barrirchar); } // 元の文字リストを"@"しかない文字リストの中央にコピーする for (int i = 0; i < ROW_COUNT; i++) { - System.arraycopy(ORIGIN_CIPHER[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); + System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); } return paddedCipher; } @@ -74,14 +74,14 @@ public class SpiralDecoder { * 周りをNULLで埋めた文字リストの解読 @にぶつかったら方向転換するイメージでうずまき状に要素を移動する * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える * - * @param ROW_COUNT 文字列リストの行数 - * @param COLUMN_COUNT 文字列リストの列数 - * @param START_INDEX 解読後読み始める文字の順番 - * @param END_INDEX 解読後読み終える文字の順番 + * @param rowCount 文字列リストの行数 + * @param columnCount 文字列リストの列数 + * @param startIndex 解読後読み始める文字の順番 + * @param endIndex 解読後読み終える文字の順番 */ - public static String decodingCipher(final int ROW_COUNT, final int COLUMN_COUNT, - final int START_INDEX, - final int END_INDEX, final String[][] CIPHER, final String BARRIR_CHAR) { + public static String decodingCipher(final int rowCount, final int columnCount, + final int startIndex, + final int endIndex, final String[][] cipher, final String barrirchar) { // 現在いる要素を示すカーソルとして使うクラス DecodeCursor cursor = new DecodeCursor(); // NULLでかこっているため、始まりのインデックスは行、列ともに1から @@ -92,25 +92,25 @@ public class SpiralDecoder { // 現在移し終えた文字数のカウンタ int charCounter = 0; // この配列に文字移して最終的には復号した順番に文字が並ぶ - String[] decodedResult = new String[ROW_COUNT * COLUMN_COUNT]; + String[] decodedResult = new String[rowCount * columnCount]; // 初期位置から初めて@にぶつかるまでの解読処理 - while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { - decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; - CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { + decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; + cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; charCounter++; cursor.shifting(); } cursor.changeDirection(); // 初めて@にぶつかってから解読し終わるまでの処理 - while (charCounter < ROW_COUNT * COLUMN_COUNT) { + while (charCounter < rowCount * columnCount) { // @の要素に移動した際、カーソルの位置を戻す処理 cursor.modifying(); // @のぶつかるまで一方向に要素を読み取り続ける - while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { - decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; - CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { + decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; + cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; charCounter++; // カーソルを現在進むべき方向に移動させる cursor.shifting(); @@ -118,7 +118,7 @@ public class SpiralDecoder { // 方向転換の処理 cursor.changeDirection(); } - return String.join("", decodedResult).substring(START_INDEX - 1, END_INDEX); + return String.join("", decodedResult).substring(startIndex - 1, endIndex); } } @@ -132,6 +132,10 @@ class DecodeCursor { private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している private int rowIndex;// 現在の行の位置 private int columnIndex;// 現在の列の位置 + final static int RIGHT = 0; + final static int DOWN = 1; + final static int LEFT = 2; + final static int UP = 3; public int getDirection() { return direction; @@ -165,26 +169,22 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別する // 0:右 1:下 2:左 3:上 public void modifying() { - final int DIRECTION_NUM = this.direction % 4; - final int RIGHT_NUM = 0; - final int DOWN_NUM = 1; - final int LEFT_NUM = 2; - final int UP_NUM = 3; - - switch (DIRECTION_NUM) { - case RIGHT_NUM: + final int directionNum = this.direction % 4; + + switch (directionNum) { + case RIGHT: this.columnIndex++; this.rowIndex++; break; - case DOWN_NUM: + case DOWN: this.columnIndex--; this.rowIndex++; break; - case LEFT_NUM: + case LEFT: this.columnIndex--; this.rowIndex--; break; - case UP_NUM: + case UP: this.columnIndex++; this.rowIndex--; break; @@ -195,23 +195,20 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別している // 0:右 1:下 2:左 3:上 public void shifting() { - final int DIRECTION_NUM = this.direction % 4; - final int RIGHT_NUM = 0; - final int DOWN_NUM = 1; - final int LEFT_NUM = 2; - final int UP_NUM = 3; - - switch (DIRECTION_NUM) { - case RIGHT_NUM: + final int directionNum= this.direction % 4; + + + switch (directionNum) { + case RIGHT: this.columnIndex++; break; - case DOWN_NUM: + case DOWN: this.rowIndex++; break; - case LEFT_NUM: + case LEFT: this.columnIndex--; break; - case UP_NUM: + case UP: this.rowIndex--; break; } -- GitLab From 72ac6f64abff7648a28da76d14f4de58c3780580 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Tue, 8 Jul 2025 15:55:56 +0900 Subject: [PATCH 04/11] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB128?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 216 ------------------ .../src/TwoDimensionBarcodeConversion.java | 173 ++++++++++++++ 2 files changed, 173 insertions(+), 216 deletions(-) delete mode 100644 kkanazawa/src/SpiralDecoder.java create mode 100644 kkanazawa/src/TwoDimensionBarcodeConversion.java diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java deleted file mode 100644 index 45c2046..0000000 --- a/kkanazawa/src/SpiralDecoder.java +++ /dev/null @@ -1,216 +0,0 @@ -package paiza.src; - -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.Scanner; - -/** - * 受け取った二次元の文字リストをうずまき型で解読する - * - * @author 金澤継心 - */ -public class SpiralDecoder { - - public static void main(final String[] args) { - Scanner scanner = new Scanner(System.in); - final int rowCount = scanner.nextInt(); // 入力値の行の数 - final int columnCount = scanner.nextInt(); // 入力値の列の数; - final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス - final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス - final String barrirchar = "@"; - - // もととなる二次元の文字リストのを入力から受け取る - final String[][] cipher = inputCipher(scanner, rowCount, columnCount); - scanner.close(); - - // 入力値の二次元の文字リストの周りをNULLで埋める - final String[][] paddedCipher = paddingCipher(cipher, barrirchar); - - // 解読処理 - final String selectedDecode = decodingCipher(rowCount, columnCount, startIndex, endIndex, - paddedCipher, barrirchar); - System.out.println(selectedDecode); - } - - /** - * もととなる文字列を入力から受け取る - * - * @param scanner 入力を受け取るスキャナー - * @param rowCount 文字リストの行数 - * @param columnCount 文字リストの列数 - */ - public static String[][] inputCipher(final Scanner scanner, final int rowCount, - final int columnCount) { - String[][] cipher = new String[rowCount][columnCount]; - for (int i = 0; i < rowCount; i++) { - String inputRow = scanner.next(); - cipher[i] = inputRow.split(""); - } - return cipher; - } - - /** - * 文字リストの周りを@で埋める - * - * @param originCipher もととなる文字リスト - */ - public static String[][] paddingCipher(final String[][] originCipher, final String barrirchar) { - final int ROW_COUNT = Array.getLength(originCipher); - final int COLUMN_COUNT = Array.getLength(originCipher[0]); - String[][] paddedCipher = new String[ROW_COUNT + 2][COLUMN_COUNT + 2]; - - for (String[] o : paddedCipher) { - Arrays.fill(o, barrirchar); - } - - // 元の文字リストを"@"しかない文字リストの中央にコピーする - for (int i = 0; i < ROW_COUNT; i++) { - System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); - } - return paddedCipher; - } - - /** - * 周りをNULLで埋めた文字リストの解読 @にぶつかったら方向転換するイメージでうずまき状に要素を移動する - * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える - * - * @param rowCount 文字列リストの行数 - * @param columnCount 文字列リストの列数 - * @param startIndex 解読後読み始める文字の順番 - * @param endIndex 解読後読み終える文字の順番 - */ - public static String decodingCipher(final int rowCount, final int columnCount, - final int startIndex, - final int endIndex, final String[][] cipher, final String barrirchar) { - // 現在いる要素を示すカーソルとして使うクラス - DecodeCursor cursor = new DecodeCursor(); - // NULLでかこっているため、始まりのインデックスは行、列ともに1から - cursor.setRowIndex(1); - cursor.setColumnIndex(1); - // 移動する方向を表すインスタンス変数 - cursor.setDirection(0); - // 現在移し終えた文字数のカウンタ - int charCounter = 0; - // この配列に文字移して最終的には復号した順番に文字が並ぶ - String[] decodedResult = new String[rowCount * columnCount]; - - // 初期位置から初めて@にぶつかるまでの解読処理 - while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { - decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; - cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; - charCounter++; - cursor.shifting(); - } - cursor.changeDirection(); - - // 初めて@にぶつかってから解読し終わるまでの処理 - while (charCounter < rowCount * columnCount) { - // @の要素に移動した際、カーソルの位置を戻す処理 - cursor.modifying(); - // @のぶつかるまで一方向に要素を読み取り続ける - while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { - decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; - cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; - charCounter++; - // カーソルを現在進むべき方向に移動させる - cursor.shifting(); - } - // 方向転換の処理 - cursor.changeDirection(); - } - return String.join("", decodedResult).substring(startIndex - 1, endIndex); - } - -} - - -/** - * 現在読み取っている要素を示すカーソルとして使うクラス - * - */ -class DecodeCursor { - private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している - private int rowIndex;// 現在の行の位置 - private int columnIndex;// 現在の列の位置 - final static int RIGHT = 0; - final static int DOWN = 1; - final static int LEFT = 2; - final static int UP = 3; - - public int getDirection() { - return direction; - } - - public void setDirection(final int DIRECTION) { - this.direction = DIRECTION; - } - - public void changeDirection() { - this.direction++; - } - - public int getRowIndex() { - return rowIndex; - } - - public void setRowIndex(final int ROW_INDEX) { - this.rowIndex = ROW_INDEX; - } - - public int getColumnIndex() { - return columnIndex; - } - - public void setColumnIndex(final int COLUMN_INDEX) { - this.columnIndex = COLUMN_INDEX; - } - - // NULL(要検討)の要素に移動した際、カーソルの位置を戻す処理 - // 4つの方向はdirectionを4で割った剰余として区別する - // 0:右 1:下 2:左 3:上 - public void modifying() { - final int directionNum = this.direction % 4; - - switch (directionNum) { - case RIGHT: - this.columnIndex++; - this.rowIndex++; - break; - case DOWN: - this.columnIndex--; - this.rowIndex++; - break; - case LEFT: - this.columnIndex--; - this.rowIndex--; - break; - case UP: - this.columnIndex++; - this.rowIndex--; - break; - } - } - - // カーソルを現在進むべき方向に移動させる - // 4つの方向はdirectionを4で割った剰余として区別している - // 0:右 1:下 2:左 3:上 - public void shifting() { - final int directionNum= this.direction % 4; - - - switch (directionNum) { - case RIGHT: - this.columnIndex++; - break; - case DOWN: - this.rowIndex++; - break; - case LEFT: - this.columnIndex--; - break; - case UP: - this.rowIndex--; - break; - } - } -} diff --git a/kkanazawa/src/TwoDimensionBarcodeConversion.java b/kkanazawa/src/TwoDimensionBarcodeConversion.java new file mode 100644 index 0000000..8d85be2 --- /dev/null +++ b/kkanazawa/src/TwoDimensionBarcodeConversion.java @@ -0,0 +1,173 @@ +package paiza.src; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; +/** + * 受け取った数字を二次元の簡易バーコードに変換して出力する + * + * @author 金澤継心 + */ +public class TwoDimensionBarcodeConversion { + //入力値を一桁ごとに分ける際に使用 + final static int DIVISOR = 10; + + public static void main(final String[] args) { + + int inputNumber = inputNumber(); + + ArrayList allNumber = divideThreeEach(inputNumber); + + for (ThreeNumbers threeNum : allNumber) { + threeNum.outputDecodeResult(); + } + } + /** + * 整数値を入力して返す + * 整数の入力は以下の条件を満たす + * ・100 ≦ N ≦ 999,999,999 + * ・(N の桁数) = 3 の倍数 + */ + public static int inputNumber() { + int inputNum = 0; + + try(Scanner scanner = new Scanner(System.in)){ + inputNum = scanner.nextInt(); + }catch(Exception e) { + System.out.println("エラー発生"); + } + return inputNum; + } + /** + * 引数の整数値を1桁ごとに分け、それを三つずつまとめたリストを返す + * + * @param inputNumber 入力された整数値 + */ + public static ArrayList divideThreeEach(final int inputNumber){ + ArrayList splitNum = divideOneEach(inputNumber); + ArrayList allNumbers = new ArrayList<>(); + + //一桁ごとに分けられた数字を3つずつにまとめてリストに格納 + for (int count = 0; count < splitNum.size(); count += 2) { + ThreeNumbers three = new ThreeNumbers(); + three.setNumber(splitNum.get(count), splitNum.get(count + 1), splitNum.get(count + 2)); + allNumbers.add(three); + count++; + } + return allNumbers; + } + /** + * 引数の整数値を一桁ごとに分けてリストとして返す + * + * @param inputNumber 入力された整数値 + */ + public static ArrayList divideOneEach(final int inputNumber){ + int originNumber = inputNumber; + ArrayList splitNum = new ArrayList<>(); + while (originNumber > 0) { + splitNum.add(originNumber % DIVISOR); + originNumber /= DIVISOR; + } + Collections.reverse(splitNum); + + return splitNum; + } +} + +/** + * 入力値において並んだ三つの桁の値を保存するクラス + * + */ +class ThreeNumbers { + private int leftNumber; + private int centerNumber; + private int rightNumber; + + public int getLeftNumber() { + return leftNumber; + } + + public void setNumber(final int leftNumber, final int centerNumber, final int rightNumber) { + this.leftNumber = leftNumber; + this.centerNumber = centerNumber; + this.rightNumber = rightNumber; + } + + public void setLeftNumber(int leftNumber) { + this.leftNumber = leftNumber; + } + + public int getCenterNumber() { + return centerNumber; + } + + public void setCenterNumber(int centerNumber) { + this.centerNumber = centerNumber; + } + + public int getRightNumber() { + return rightNumber; + } + + public void setRightNumber(int rightNumber) { + this.rightNumber = rightNumber; + } + + public void outputDecodeResult() { + for(StringBuilder decodeNum : decodeThreeNum()) { + System.out.println(decodeNum); + } + } + /** + * 三つの数を二次元バーコードに変換して返す. + * decodeNum()で一つ一つの数字をバーコードに変換した後、 + * それらを横並びにつなげる + * + */ + public ArrayList decodeThreeNum() { + int left = leftNumber; + int center = centerNumber; + int right = rightNumber; + ArrayList leftDecode = decodeNum(left); + ArrayList centerDecode = decodeNum(center); + ArrayList rightDecode = decodeNum(right); + + ArrayList decodedThree = new ArrayList<>(); + //左、中央、右の数のバーコードを表す文字列で + //同じcount行目の文字列をつなげる + for (int count = 0; count < 3; count++) { + StringBuilder row = new StringBuilder(); + row.append(leftDecode.get(count)); + row.append(centerDecode.get(count)); + row.append(rightDecode.get(count)); + decodedThree.add(row); + } + return decodedThree; + } + /** + * 一つの数字を二次元バーコードに変換して返す. + * 二次元バーコードを三行の文字列として扱う. + */ + public ArrayList decodeNum(final int number) { + int num = number; + //decodedNumに1行目、2行目、3行目の文字列をそれぞれ入れる + ArrayList decodedNum = new ArrayList<>(); + + for (int count1 = 0; count1 < 3; count1++) { + //rowに1行分の文字列をを入れる + StringBuilder row = new StringBuilder(); + //1行文の文字列を作成 + for (int count2 = 0; count2 < 3; count2++) { + if (0 < num) { + row.append("#"); + } else { + row.append("."); + } + num--; + } + decodedNum.add(row); + } + return decodedNum; + } + +} -- GitLab From e33dab918063d5d944c51a770155d3f3bef8ac03 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Tue, 8 Jul 2025 16:21:03 +0900 Subject: [PATCH 05/11] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB128?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/TwoDimensionBarcodeConversion.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/kkanazawa/src/TwoDimensionBarcodeConversion.java b/kkanazawa/src/TwoDimensionBarcodeConversion.java index 8d85be2..f40db7b 100644 --- a/kkanazawa/src/TwoDimensionBarcodeConversion.java +++ b/kkanazawa/src/TwoDimensionBarcodeConversion.java @@ -14,9 +14,9 @@ public class TwoDimensionBarcodeConversion { public static void main(final String[] args) { - int inputNumber = inputNumber(); + final int inputNumber = inputNumber(); - ArrayList allNumber = divideThreeEach(inputNumber); + final ArrayList allNumber = divideThreeEach(inputNumber); for (ThreeNumbers threeNum : allNumber) { threeNum.outputDecodeResult(); @@ -44,7 +44,7 @@ public class TwoDimensionBarcodeConversion { * @param inputNumber 入力された整数値 */ public static ArrayList divideThreeEach(final int inputNumber){ - ArrayList splitNum = divideOneEach(inputNumber); + final ArrayList splitNum = divideOneEach(inputNumber); ArrayList allNumbers = new ArrayList<>(); //一桁ごとに分けられた数字を3つずつにまとめてリストに格納 @@ -125,12 +125,9 @@ class ThreeNumbers { * */ public ArrayList decodeThreeNum() { - int left = leftNumber; - int center = centerNumber; - int right = rightNumber; - ArrayList leftDecode = decodeNum(left); - ArrayList centerDecode = decodeNum(center); - ArrayList rightDecode = decodeNum(right); + ArrayList leftDecode = decodeNum(leftNumber); + ArrayList centerDecode = decodeNum(centerNumber); + ArrayList rightDecode = decodeNum(rightNumber); ArrayList decodedThree = new ArrayList<>(); //左、中央、右の数のバーコードを表す文字列で -- GitLab From 20bf65f60263573c4d54a017d2b09053b76528a6 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Tue, 8 Jul 2025 17:12:08 +0900 Subject: [PATCH 06/11] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB128?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...java => TwoDimensionBarcodeConverter.java} | 64 ++++++++----------- 1 file changed, 26 insertions(+), 38 deletions(-) rename kkanazawa/src/{TwoDimensionBarcodeConversion.java => TwoDimensionBarcodeConverter.java} (67%) diff --git a/kkanazawa/src/TwoDimensionBarcodeConversion.java b/kkanazawa/src/TwoDimensionBarcodeConverter.java similarity index 67% rename from kkanazawa/src/TwoDimensionBarcodeConversion.java rename to kkanazawa/src/TwoDimensionBarcodeConverter.java index f40db7b..43fbd51 100644 --- a/kkanazawa/src/TwoDimensionBarcodeConversion.java +++ b/kkanazawa/src/TwoDimensionBarcodeConverter.java @@ -2,13 +2,14 @@ package paiza.src; import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Scanner; /** * 受け取った数字を二次元の簡易バーコードに変換して出力する * * @author 金澤継心 */ -public class TwoDimensionBarcodeConversion { +public class TwoDimensionBarcodeConverter { //入力値を一桁ごとに分ける際に使用 final static int DIVISOR = 10; @@ -16,7 +17,7 @@ public class TwoDimensionBarcodeConversion { final int inputNumber = inputNumber(); - final ArrayList allNumber = divideThreeEach(inputNumber); + final List allNumber = divideThreeEach(inputNumber); for (ThreeNumbers threeNum : allNumber) { threeNum.outputDecodeResult(); @@ -43,14 +44,13 @@ public class TwoDimensionBarcodeConversion { * * @param inputNumber 入力された整数値 */ - public static ArrayList divideThreeEach(final int inputNumber){ - final ArrayList splitNum = divideOneEach(inputNumber); - ArrayList allNumbers = new ArrayList<>(); + public static List divideThreeEach(final int inputNumber){ + final List splitNum = divideOneEach(inputNumber); + List allNumbers = new ArrayList<>(); //一桁ごとに分けられた数字を3つずつにまとめてリストに格納 for (int count = 0; count < splitNum.size(); count += 2) { - ThreeNumbers three = new ThreeNumbers(); - three.setNumber(splitNum.get(count), splitNum.get(count + 1), splitNum.get(count + 2)); + ThreeNumbers three = new ThreeNumbers(splitNum.get(count),splitNum.get(count+1),splitNum.get(count+2)); allNumbers.add(three); count++; } @@ -61,9 +61,9 @@ public class TwoDimensionBarcodeConversion { * * @param inputNumber 入力された整数値 */ - public static ArrayList divideOneEach(final int inputNumber){ + public static List divideOneEach(final int inputNumber){ int originNumber = inputNumber; - ArrayList splitNum = new ArrayList<>(); + List splitNum = new ArrayList<>(); while (originNumber > 0) { splitNum.add(originNumber % DIVISOR); originNumber /= DIVISOR; @@ -82,37 +82,25 @@ class ThreeNumbers { private int leftNumber; private int centerNumber; private int rightNumber; - - public int getLeftNumber() { - return leftNumber; - } - - public void setNumber(final int leftNumber, final int centerNumber, final int rightNumber) { + + public ThreeNumbers(int leftNumber, int centerNumber, int rightNumber) { this.leftNumber = leftNumber; this.centerNumber = centerNumber; this.rightNumber = rightNumber; } - public void setLeftNumber(int leftNumber) { - this.leftNumber = leftNumber; + public int getLeftNumber() { + return leftNumber; } public int getCenterNumber() { return centerNumber; } - public void setCenterNumber(int centerNumber) { - this.centerNumber = centerNumber; - } - public int getRightNumber() { return rightNumber; } - public void setRightNumber(int rightNumber) { - this.rightNumber = rightNumber; - } - public void outputDecodeResult() { for(StringBuilder decodeNum : decodeThreeNum()) { System.out.println(decodeNum); @@ -124,12 +112,12 @@ class ThreeNumbers { * それらを横並びにつなげる * */ - public ArrayList decodeThreeNum() { - ArrayList leftDecode = decodeNum(leftNumber); - ArrayList centerDecode = decodeNum(centerNumber); - ArrayList rightDecode = decodeNum(rightNumber); + public List decodeThreeNum() { + List leftDecode = decodeSingleNum(leftNumber); + List centerDecode = decodeSingleNum(centerNumber); + List rightDecode = decodeSingleNum(rightNumber); - ArrayList decodedThree = new ArrayList<>(); + List decodedThreeNum = new ArrayList<>(); //左、中央、右の数のバーコードを表す文字列で //同じcount行目の文字列をつなげる for (int count = 0; count < 3; count++) { @@ -137,24 +125,24 @@ class ThreeNumbers { row.append(leftDecode.get(count)); row.append(centerDecode.get(count)); row.append(rightDecode.get(count)); - decodedThree.add(row); + decodedThreeNum.add(row); } - return decodedThree; + return decodedThreeNum; } /** * 一つの数字を二次元バーコードに変換して返す. * 二次元バーコードを三行の文字列として扱う. */ - public ArrayList decodeNum(final int number) { + public List decodeSingleNum(final int number) { int num = number; //decodedNumに1行目、2行目、3行目の文字列をそれぞれ入れる - ArrayList decodedNum = new ArrayList<>(); - - for (int count1 = 0; count1 < 3; count1++) { - //rowに1行分の文字列をを入れる + List decodedNum = new ArrayList<>(); + //上から1行目、2行目、3行目の順に文字列を作成 + for (int rowindex = 0; rowindex < 3; rowindex++) { + //rowに1行分の文字列を入れる StringBuilder row = new StringBuilder(); //1行文の文字列を作成 - for (int count2 = 0; count2 < 3; count2++) { + for (int columnIndex = 0; columnIndex < 3; columnIndex++) { if (0 < num) { row.append("#"); } else { -- GitLab From cf397ef97ff249e6c80b61d9226a76fa38128ba5 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Wed, 23 Jul 2025 16:35:20 +0900 Subject: [PATCH 07/11] =?UTF-8?q?paizaB153=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/Katori.java | 236 +++++++++++++++++++++++++++++++++ kkanazawa/test/KatoriTest.java | 204 ++++++++++++++++++++++++++++ 2 files changed, 440 insertions(+) create mode 100644 kkanazawa/src/Katori.java create mode 100644 kkanazawa/test/KatoriTest.java diff --git a/kkanazawa/src/Katori.java b/kkanazawa/src/Katori.java new file mode 100644 index 0000000..c3dd0f4 --- /dev/null +++ b/kkanazawa/src/Katori.java @@ -0,0 +1,236 @@ +package paiza.src; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +/** + * + * + */ +public class Katori { + + public static void main(final String[] args) { + Scanner scanner = new Scanner(System.in); + final int sideLength = scanner.nextInt(); // 入力値の行,列の数 + + // もととなる二次元配列を入力から受け取る + final int[][] coil = KatoriIO.inputCoil(scanner, sideLength); + scanner.close(); + + // 入力値の二次元配列の周りを-1で埋める + final int[][] paddedCoil = CalcKatori.paddingCoil(coil, sideLength); + + // 二次元配列を渦巻き状に読み取り一次元リストに保存 + final List decodedKatori = CalcKatori.decodeKatori(sideLength, paddedCoil); + + // 渦巻き状に読み取ったリストから1/4, 2/4, 3/4, すべて燃えきる,までにかかる時間を計算 + final List fourTimes = CalcKatori.calcurateTime(decodedKatori); + + // 計算結果の表示 + KatoriIO.outputTimes(fourTimes); + } + +} + + +/** + * 入出力を行うクラス + * + */ +final class KatoriIO { + /** + * 蚊取り線香のブロックごとに燃える時間を二次元配列として入力する + * + * @param scanner + * @param sideLength 文字リストの行数、列数 + */ + public static int[][] inputCoil(final Scanner scanner, final int sideLength) { + int[][] coil = new int[sideLength][sideLength]; + for (int row = 0; row < sideLength; row++) { + for (int column = 0; column < sideLength; column++) { + coil[row][column] = scanner.nextInt(); + } + } + return coil; + } + + /** + * 蚊取り線香が1/4, 2/4, 3/4, すべて燃えきる,までにかかる時間を表示する + * + */ + public static void outputTimes(List fourTimes) { + for (Integer time : fourTimes) { + System.out.println(time); + } + + } +} + + +final class CalcKatori { + // 二次元配列を囲う値 + final static int BARRIER_NUM = -1; + + /** + * 二次元配列の周りを"-1"で埋める + * + * @param originCipher もととなる文字リスト + */ + public static int[][] paddingCoil(final int[][] originCoil, final int sideLength) { + + int[][] paddedCoil = new int[sideLength + 2][sideLength + 2]; + + for (int[] o : paddedCoil) { + Arrays.fill(o, BARRIER_NUM); + } + // 元リストを"-1"しかない文字リストの中央にコピーする + for (int row = 0; row < sideLength; row++) { + System.arraycopy(originCoil[row], 0, paddedCoil[row + 1], 1, sideLength); + } + return paddedCoil; + } + + /** + * 周りを"-1"で埋めた文字リストの解読 "-1"にぶつかったら方向転換するイメージでうずまき状に要素を移動する 移動した順に二次元配列の要素を1次元の別のリストに移していく + * 読み取った二次元配列の要素は"-1"に置き換える + * + * @param sideLength 文字列リストの行数、列数 + * @param coil 二次元配列 + */ + public static List decodeKatori(final int sideLength, final int[][] coil) { + + // "-1"でかこっているため、始まりのインデックスは行、列ともに1から + IndexPointer pointer = new IndexPointer(1, 1, Direction.RIGHT); + + // 現在移し終えた要素のカウンタ + int brockCounter = 0; + // この配列に要素を移して最終的にはうずまきの順番に値が並ぶ + List brockTimes = new ArrayList<>(); + + // 初期位置から初めて-1にぶつかるまでの解読処理 + while (coil[pointer.getRowIndex()][pointer.getColumnIndex()] != BARRIER_NUM) { + brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); + coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; + brockCounter++; + pointer.shifting(); + } + + // 初めて-1にぶつかってから解読し終わるまでの処理 + while (brockCounter < sideLength * sideLength) { + // -1の要素に移動した際、カーソルの位置を戻す処理 + pointer.modifying(); + // -1にぶつかるまで一方向に要素を読み取り続ける + while (coil[pointer.getRowIndex()][pointer.getColumnIndex()] != BARRIER_NUM) { + brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); + coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; + brockCounter++; + // カーソルを現在進んでいる方向に移動させる + pointer.shifting(); + } + } + + return brockTimes; + + } + + public static List calcurateTime(List brockTimes) { + // 読み取った値を全体の1/4, 2/4, 3/4, 4/4、ごとに和をとり、リストに格納 + List fourTimes = new ArrayList<>(); + fourTimes.add(brockTimes.subList(0, brockTimes.size() / 4).stream().reduce(Integer::sum).get()); + fourTimes.add(brockTimes.subList(0, brockTimes.size() / 2).stream().reduce(Integer::sum).get()); + fourTimes + .add(brockTimes.subList(0, brockTimes.size() * 3 / 4).stream().reduce(Integer::sum).get()); + fourTimes.add(brockTimes.subList(0, brockTimes.size()).stream().reduce(Integer::sum).get()); + return fourTimes; + } +} + + +/** + * 要素を読み取る方向 + */ +enum Direction { + RIGHT, + DOWN, + LEFT, + UP, +} + + +/** + * 現在読み取っている位置を示すポインターのクラス + * + */ +final class IndexPointer { + + private int rowIndex;// 現在の行の位置 + private int columnIndex;// 現在の列の位置 + private Direction direction; + + public IndexPointer(int rowIndex, int columnIndex, Direction direction) { + this.rowIndex = rowIndex; + this.columnIndex = columnIndex; + this.direction = direction; + } + + public Direction getDirection() { + return direction; + } + + public int getRowIndex() { + return rowIndex; + } + + public int getColumnIndex() { + return columnIndex; + } + + // -1の要素に移動した際、カーソルの位置を戻す処理 + // 時計周りに移動方向を変える + public void modifying() { + + switch (direction) { + case RIGHT: + this.columnIndex--; + this.rowIndex++; + direction = Direction.DOWN; + break; + case DOWN: + this.columnIndex--; + this.rowIndex--; + direction = Direction.LEFT; + break; + case LEFT: + this.columnIndex++; + this.rowIndex--; + direction = Direction.UP; + break; + case UP: + this.columnIndex++; + this.rowIndex++; + direction = Direction.RIGHT; + break; + } + } + + // カーソルを現在進む方向に移動させる + public void shifting() { + + switch (direction) { + case RIGHT: + this.columnIndex++; + break; + case DOWN: + this.rowIndex++; + break; + case LEFT: + this.columnIndex--; + break; + case UP: + this.rowIndex--; + break; + } + } +} diff --git a/kkanazawa/test/KatoriTest.java b/kkanazawa/test/KatoriTest.java new file mode 100644 index 0000000..3e0c1cd --- /dev/null +++ b/kkanazawa/test/KatoriTest.java @@ -0,0 +1,204 @@ +package paiza.src; + +import static org.junit.Assert.*; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +public class KatoriTest { + + //二次元配列の周りを囲う値 + final static int BARRIER_NUM = -1; + //二次元配列の行数、列数 + final static int SIDE_LENGTH = 4; + + public static class KatoriIOTest { + + + @Test + public void OutputTimesに12_39_57_82_を格納したリストを渡すと各要素が改行を入れて出力される() { + String expected = 12 + + System.lineSeparator() + + + 39 + + System.lineSeparator() + + + 57 + + System.lineSeparator() + + + 82 + +System.lineSeparator(); + + List fourTimes = new ArrayList<>(); + fourTimes.add(12); + fourTimes.add(39); + fourTimes.add(57); + fourTimes.add(82); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + KatoriIO.outputTimes(fourTimes); + + assertEquals(expected,out.toString()); + } + } + + public static class CalckatoriTest { + int[][] coil; + int[][] paddingCoil; + List decodedKatori; + List times; + + @Before + public void setUp() { + coil = new int[][] { + { + 3, 4, 3, 2 + }, { + 7, 9, 3, 7 + }, { + 1, 9, 4, 6 + }, { + 3, 7, 8, 6 + } + }; + + paddingCoil = new int[][] { + { + -1, -1, -1, -1, -1, -1 + }, { + -1, 3, 4, 3, 2, -1 + }, { + -1, 7, 9, 3, 7, -1 + }, { + -1, 1, 9, 4, 6, -1 + }, { + -1, 3, 7, 8, 6, -1 + }, { + -1, -1, -1, -1, -1, -1 + } + }; + + decodedKatori = new ArrayList<>(); + decodedKatori.add(3); + decodedKatori.add(4); + decodedKatori.add(3); + decodedKatori.add(2); + decodedKatori.add(7); + decodedKatori.add(6); + decodedKatori.add(6); + decodedKatori.add(8); + decodedKatori.add(7); + decodedKatori.add(3); + decodedKatori.add(1); + decodedKatori.add(7); + decodedKatori.add(9); + decodedKatori.add(3); + decodedKatori.add(4); + decodedKatori.add(9); + + times = new ArrayList<>(); + times.add(12); + times.add(39); + times.add(57); + times.add(82); + } + + @Test + public void PaddingCoilに4行4列の二次元配列coilを渡すとその二次元配列の周りをBARRIER_NUMで囲った二次元配列を返す() { + int[][] result = CalcKatori.paddingCoil(coil, SIDE_LENGTH); + for (int row = 0; row < SIDE_LENGTH + 2; row++) { + for (int column = 0; column < SIDE_LENGTH + 2; column++) { + assertEquals(paddingCoil[row][column], result[row][column]); + } + } + } + + @Test + public void decodedKatoriに二次元配列paddingCoilを渡すとうずまき状に要素を読み取ったリスト返す() { + List result = CalcKatori.decodeKatori(SIDE_LENGTH, paddingCoil); + assertEquals(decodedKatori, result); + } + + @Test + public void calcurateTimeに1次元リストdecodedKatoriを渡すと四分の一_四分の二_四分の三_四分の四ごとに和をとった値のリストを返す() { + List result = CalcKatori.calcurateTime(decodedKatori); + assertEquals(times, result); + } + } + + public static class IndexPointerTest { + + @Test + public void コンストラクタで設定した値がゲッターで取得できる() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); + assertEquals(1, pointer.getRowIndex()); + assertEquals(5, pointer.getColumnIndex()); + assertEquals(Direction.RIGHT, pointer.getDirection()); + + } + + @Test + public void modifyingでRIGHTから時計回りに1周してRIGHTに戻ってくる() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); + assertEquals(Direction.RIGHT, pointer.getDirection()); + pointer.modifying(); + assertEquals(Direction.DOWN, pointer.getDirection()); + pointer.modifying(); + assertEquals(Direction.LEFT, pointer.getDirection()); + pointer.modifying(); + assertEquals(Direction.UP, pointer.getDirection()); + pointer.modifying(); + assertEquals(Direction.RIGHT, pointer.getDirection()); + + } + + @RunWith(Theories.class) + public static class shiftingのテスト { + @DataPoints + public static Fixture[] PARAMS = { + new Fixture(1, 5, Direction.RIGHT, 1, 6), + new Fixture(1, 5, Direction.DOWN, 2, 5), + new Fixture(1, 5, Direction.LEFT, 1, 4), + new Fixture(1, 5, Direction.UP, 0, 5), + }; + @Theory + //RIGHT:行インデックス+0 列インデックス+1 + //DOWN:行インデックス+1 列インデックス+0 + //LEFT:行インデックス+0 列インデックスー1 + //UP:行インデックスー1 列インデックス+0 + public void RIGHT_DOWN_LEFT_UPに対してそれぞれインデックスが適切に変動(Fixture params) throws Exception { + IndexPointer pointer = new IndexPointer(params.indexRow, params.indexColumn, params.direction); + pointer.shifting(); + assertEquals(params.expectedRow, pointer.getRowIndex()); + assertEquals(params.expectedColumn, pointer.getColumnIndex()); + } + + //パラメータ化用のクラス + static class Fixture { + int indexRow; + int indexColumn; + Direction direction; + int expectedRow; + int expectedColumn; + + Fixture(int indexRow, int indexColumn, Direction direction, int expectedRow, int expectedColumn) { + this.indexRow = indexRow; + this.indexColumn = indexColumn; + this.direction = direction; + this.expectedRow = expectedRow; + this.expectedColumn = expectedColumn; + } + } + + } + + + } +} -- GitLab From 74b415c4803ec66ce0f5332a366745b3ecaea207 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Wed, 23 Jul 2025 16:38:47 +0900 Subject: [PATCH 08/11] =?UTF-8?q?paizaB153=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/TwoDimensionBarcodeConverter.java | 158 ------------------ 1 file changed, 158 deletions(-) delete mode 100644 kkanazawa/src/TwoDimensionBarcodeConverter.java diff --git a/kkanazawa/src/TwoDimensionBarcodeConverter.java b/kkanazawa/src/TwoDimensionBarcodeConverter.java deleted file mode 100644 index 43fbd51..0000000 --- a/kkanazawa/src/TwoDimensionBarcodeConverter.java +++ /dev/null @@ -1,158 +0,0 @@ -package paiza.src; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Scanner; -/** - * 受け取った数字を二次元の簡易バーコードに変換して出力する - * - * @author 金澤継心 - */ -public class TwoDimensionBarcodeConverter { - //入力値を一桁ごとに分ける際に使用 - final static int DIVISOR = 10; - - public static void main(final String[] args) { - - final int inputNumber = inputNumber(); - - final List allNumber = divideThreeEach(inputNumber); - - for (ThreeNumbers threeNum : allNumber) { - threeNum.outputDecodeResult(); - } - } - /** - * 整数値を入力して返す - * 整数の入力は以下の条件を満たす - * ・100 ≦ N ≦ 999,999,999 - * ・(N の桁数) = 3 の倍数 - */ - public static int inputNumber() { - int inputNum = 0; - - try(Scanner scanner = new Scanner(System.in)){ - inputNum = scanner.nextInt(); - }catch(Exception e) { - System.out.println("エラー発生"); - } - return inputNum; - } - /** - * 引数の整数値を1桁ごとに分け、それを三つずつまとめたリストを返す - * - * @param inputNumber 入力された整数値 - */ - public static List divideThreeEach(final int inputNumber){ - final List splitNum = divideOneEach(inputNumber); - List allNumbers = new ArrayList<>(); - - //一桁ごとに分けられた数字を3つずつにまとめてリストに格納 - for (int count = 0; count < splitNum.size(); count += 2) { - ThreeNumbers three = new ThreeNumbers(splitNum.get(count),splitNum.get(count+1),splitNum.get(count+2)); - allNumbers.add(three); - count++; - } - return allNumbers; - } - /** - * 引数の整数値を一桁ごとに分けてリストとして返す - * - * @param inputNumber 入力された整数値 - */ - public static List divideOneEach(final int inputNumber){ - int originNumber = inputNumber; - List splitNum = new ArrayList<>(); - while (originNumber > 0) { - splitNum.add(originNumber % DIVISOR); - originNumber /= DIVISOR; - } - Collections.reverse(splitNum); - - return splitNum; - } -} - -/** - * 入力値において並んだ三つの桁の値を保存するクラス - * - */ -class ThreeNumbers { - private int leftNumber; - private int centerNumber; - private int rightNumber; - - public ThreeNumbers(int leftNumber, int centerNumber, int rightNumber) { - this.leftNumber = leftNumber; - this.centerNumber = centerNumber; - this.rightNumber = rightNumber; - } - - public int getLeftNumber() { - return leftNumber; - } - - public int getCenterNumber() { - return centerNumber; - } - - public int getRightNumber() { - return rightNumber; - } - - public void outputDecodeResult() { - for(StringBuilder decodeNum : decodeThreeNum()) { - System.out.println(decodeNum); - } - } - /** - * 三つの数を二次元バーコードに変換して返す. - * decodeNum()で一つ一つの数字をバーコードに変換した後、 - * それらを横並びにつなげる - * - */ - public List decodeThreeNum() { - List leftDecode = decodeSingleNum(leftNumber); - List centerDecode = decodeSingleNum(centerNumber); - List rightDecode = decodeSingleNum(rightNumber); - - List decodedThreeNum = new ArrayList<>(); - //左、中央、右の数のバーコードを表す文字列で - //同じcount行目の文字列をつなげる - for (int count = 0; count < 3; count++) { - StringBuilder row = new StringBuilder(); - row.append(leftDecode.get(count)); - row.append(centerDecode.get(count)); - row.append(rightDecode.get(count)); - decodedThreeNum.add(row); - } - return decodedThreeNum; - } - /** - * 一つの数字を二次元バーコードに変換して返す. - * 二次元バーコードを三行の文字列として扱う. - */ - public List decodeSingleNum(final int number) { - int num = number; - //decodedNumに1行目、2行目、3行目の文字列をそれぞれ入れる - List decodedNum = new ArrayList<>(); - //上から1行目、2行目、3行目の順に文字列を作成 - for (int rowindex = 0; rowindex < 3; rowindex++) { - //rowに1行分の文字列を入れる - StringBuilder row = new StringBuilder(); - //1行文の文字列を作成 - for (int columnIndex = 0; columnIndex < 3; columnIndex++) { - if (0 < num) { - row.append("#"); - } else { - row.append("."); - } - num--; - } - decodedNum.add(row); - } - return decodedNum; - } - -} -- GitLab From f5cd24789341eac597782f77ac39e6d9ba40cb49 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Fri, 25 Jul 2025 12:56:52 +0900 Subject: [PATCH 09/11] =?UTF-8?q?painza=E3=81=AE=E5=95=8F=E9=A1=8C?= =?UTF-8?q?=E3=81=AEB054=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/AlphabetAddition.java | 213 ++++++++++++++++++++ kkanazawa/src/Katori.java | 236 ----------------------- kkanazawa/test/AlphabetAdditionTest.java | 194 +++++++++++++++++++ kkanazawa/test/KatoriTest.java | 204 -------------------- 4 files changed, 407 insertions(+), 440 deletions(-) create mode 100644 kkanazawa/src/AlphabetAddition.java delete mode 100644 kkanazawa/src/Katori.java create mode 100644 kkanazawa/test/AlphabetAdditionTest.java delete mode 100644 kkanazawa/test/KatoriTest.java diff --git a/kkanazawa/src/AlphabetAddition.java b/kkanazawa/src/AlphabetAddition.java new file mode 100644 index 0000000..b9e9e14 --- /dev/null +++ b/kkanazawa/src/AlphabetAddition.java @@ -0,0 +1,213 @@ +package paiza.src; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Scanner; + +public class AlphabetAddition { + + public static void main(final String[] args) { + + // 入力処理 + final AdditionVal alphabetPair = AlphabetIO.inputVal(); + + // 入力値の和の計算&計算結果の出力処理 + System.out.println(alphabetPair.alphabetSum()); + + } +} + +/** + * 入力処理、出力処理を行うクラス + * + */ +final class AlphabetIO { + + // 二つのアルファベットの値を入力し、それらをまとめるadditioValクラスとして返す + public static AdditionVal inputVal() { + String firstVal = new String(); + String secondVal = new String(); + + // 入力処理 + try (Scanner scanner = new Scanner(System.in)) { + firstVal = scanner.next(); + secondVal = scanner.next(); + } catch (NoSuchElementException e) { + throw new NoSuchElementException("エラー:入力値がありません"); + } + // 入力値が単一の"A"でないなら、一文字目は"A"でないことを確認 + if (isIncorrectValue(firstVal) || isIncorrectValue(secondVal)) { + System.out.println("不正な値です"); + } + + final AdditionVal additionNum = new AdditionVal(firstVal, secondVal); + + return additionNum; + } + + // 入力値が適切な値か判定するメソッド + // 適切な値は「入力値が単一の"A"でないなら、一文字目は"A"でない」を満たす + public static boolean isIncorrectValue(String val) { + if (val.equals("A")) { + return false; + } else { + + return val.split("")[0].equals("A"); + } + } + +} + + +/** + * アルファベットのペアの和をとるクラス + * + */ +final class AdditionVal { + private String firstVal; + private String secondVal; + + public AdditionVal(final String firstNum, final String secondNum) { + this.firstVal = new String(firstNum); + this.secondVal = new String(secondNum); + } + + public String getFirstVal() { + return firstVal; + } + + public String getSecondVal() { + return secondVal; + } + + // 二つのアルファベットの値を一度10進数に変換して和をとった後、再びアルファベットに直して返す + public String alphabetSum() { + AlphabetToNum firstFix = new AlphabetToNum(firstVal); + AlphabetToNum secondFix = new AlphabetToNum(secondVal); + + // 10進数に変換した値の和をとる + final int decimalSum = firstFix.getDecimalNum() + secondFix.getDecimalNum(); + + // 10進数の和の値をアルファベットに変換して返す + return new NumToAlphabet(decimalSum).getAlphabetVal(); + } + +} + + +/** + * アルファベットから10進数に変換するクラス + * + */ +final class AlphabetToNum { + // アルファベットと5進数の対応 + private final static Map quinaryFix = new HashMap<>() { + { + put("A", 0); + put("B", 1); + put("C", 2); + put("D", 3); + put("E", 4); + } + }; + + // 5進数を10進数に変換する際に使用する基数 + static final int FUND_NUM = 5; + private final String AlphabetVal; + private final int quinaryNum; + private final int decimalNum; + + public AlphabetToNum(final String AlphabetNum) { + this.AlphabetVal = AlphabetNum; + this.quinaryNum = alphabetToQuinary(AlphabetVal); + this.decimalNum = quinaryToDecimal(quinaryNum); + } + + public int getDecimalNum() { + return decimalNum; + } + + // アルファベットを5進数に変換して返す + public int alphabetToQuinary(String AlphabetVal) { + int quinaryNum = 0; + String[] firstSplit = AlphabetVal.split(""); + // 桁数のカウント + int digitsCount = firstSplit.length - 1; + + // アルファベットに対応する5進数を桁の大きい方から順に変換 + for (String str : firstSplit) { + quinaryNum += quinaryFix.get(str) * (int) Math.pow(10, digitsCount); + digitsCount--; + } + return quinaryNum; + } + + // 5進数から10進数に変換して返す + public int quinaryToDecimal(int quinaryNum) { + String numStr = String.valueOf(quinaryNum); + int decimalNum = Integer.parseInt(numStr, FUND_NUM); + return decimalNum; + } + +} + + +/** + * 10進数からアルファベットに変換するクラス + * + */ +final class NumToAlphabet { + + // 5進数とアルファベットの対応 + private final static Map AlphabetFix = new HashMap<>() { + { + put("0", "A"); + put("1", "B"); + put("2", "C"); + put("3", "D"); + put("4", "E"); + } + }; + + // 10進数を5進数に変換する際に使用する基数 + static final int FUND_NUM = 5; + private final int decimalNum; + private final int quinaryNum; + private final String AlphabetVal; + + public NumToAlphabet(final int decimalNum) { + this.decimalNum = decimalNum; + this.quinaryNum = decimalToQuinary(this.decimalNum); + this.AlphabetVal = quinaryToAlphabet(quinaryNum); + } + + public String getAlphabetVal() { + return AlphabetVal ; + } + + // 10進数を5進数に変換して返す + public int decimalToQuinary(int decinalNum) { + int quinaryNum = Integer.parseInt(Integer.toString(decimalNum, FUND_NUM)); + + return quinaryNum; + } + + // 5進数をアルファベットに変換して返す + public String quinaryToAlphabet(int quinaryNum) { + + String numStr = String.valueOf(quinaryNum); + List splitAlphabet = new ArrayList<>(); + + // 文字列型にした5進数をsplit()で1つずつに分け、順番にアルファベットに変換 + for (String str : numStr.split("")) { + splitAlphabet.add(AlphabetFix.get(str)); + } + // 変換したアルファベットのリストを文字列に結合 + String AlphabetVal = String.join("", splitAlphabet); + return new String(AlphabetVal); + } + +} diff --git a/kkanazawa/src/Katori.java b/kkanazawa/src/Katori.java deleted file mode 100644 index c3dd0f4..0000000 --- a/kkanazawa/src/Katori.java +++ /dev/null @@ -1,236 +0,0 @@ -package paiza.src; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Scanner; - -/** - * - * - */ -public class Katori { - - public static void main(final String[] args) { - Scanner scanner = new Scanner(System.in); - final int sideLength = scanner.nextInt(); // 入力値の行,列の数 - - // もととなる二次元配列を入力から受け取る - final int[][] coil = KatoriIO.inputCoil(scanner, sideLength); - scanner.close(); - - // 入力値の二次元配列の周りを-1で埋める - final int[][] paddedCoil = CalcKatori.paddingCoil(coil, sideLength); - - // 二次元配列を渦巻き状に読み取り一次元リストに保存 - final List decodedKatori = CalcKatori.decodeKatori(sideLength, paddedCoil); - - // 渦巻き状に読み取ったリストから1/4, 2/4, 3/4, すべて燃えきる,までにかかる時間を計算 - final List fourTimes = CalcKatori.calcurateTime(decodedKatori); - - // 計算結果の表示 - KatoriIO.outputTimes(fourTimes); - } - -} - - -/** - * 入出力を行うクラス - * - */ -final class KatoriIO { - /** - * 蚊取り線香のブロックごとに燃える時間を二次元配列として入力する - * - * @param scanner - * @param sideLength 文字リストの行数、列数 - */ - public static int[][] inputCoil(final Scanner scanner, final int sideLength) { - int[][] coil = new int[sideLength][sideLength]; - for (int row = 0; row < sideLength; row++) { - for (int column = 0; column < sideLength; column++) { - coil[row][column] = scanner.nextInt(); - } - } - return coil; - } - - /** - * 蚊取り線香が1/4, 2/4, 3/4, すべて燃えきる,までにかかる時間を表示する - * - */ - public static void outputTimes(List fourTimes) { - for (Integer time : fourTimes) { - System.out.println(time); - } - - } -} - - -final class CalcKatori { - // 二次元配列を囲う値 - final static int BARRIER_NUM = -1; - - /** - * 二次元配列の周りを"-1"で埋める - * - * @param originCipher もととなる文字リスト - */ - public static int[][] paddingCoil(final int[][] originCoil, final int sideLength) { - - int[][] paddedCoil = new int[sideLength + 2][sideLength + 2]; - - for (int[] o : paddedCoil) { - Arrays.fill(o, BARRIER_NUM); - } - // 元リストを"-1"しかない文字リストの中央にコピーする - for (int row = 0; row < sideLength; row++) { - System.arraycopy(originCoil[row], 0, paddedCoil[row + 1], 1, sideLength); - } - return paddedCoil; - } - - /** - * 周りを"-1"で埋めた文字リストの解読 "-1"にぶつかったら方向転換するイメージでうずまき状に要素を移動する 移動した順に二次元配列の要素を1次元の別のリストに移していく - * 読み取った二次元配列の要素は"-1"に置き換える - * - * @param sideLength 文字列リストの行数、列数 - * @param coil 二次元配列 - */ - public static List decodeKatori(final int sideLength, final int[][] coil) { - - // "-1"でかこっているため、始まりのインデックスは行、列ともに1から - IndexPointer pointer = new IndexPointer(1, 1, Direction.RIGHT); - - // 現在移し終えた要素のカウンタ - int brockCounter = 0; - // この配列に要素を移して最終的にはうずまきの順番に値が並ぶ - List brockTimes = new ArrayList<>(); - - // 初期位置から初めて-1にぶつかるまでの解読処理 - while (coil[pointer.getRowIndex()][pointer.getColumnIndex()] != BARRIER_NUM) { - brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); - coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; - brockCounter++; - pointer.shifting(); - } - - // 初めて-1にぶつかってから解読し終わるまでの処理 - while (brockCounter < sideLength * sideLength) { - // -1の要素に移動した際、カーソルの位置を戻す処理 - pointer.modifying(); - // -1にぶつかるまで一方向に要素を読み取り続ける - while (coil[pointer.getRowIndex()][pointer.getColumnIndex()] != BARRIER_NUM) { - brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); - coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; - brockCounter++; - // カーソルを現在進んでいる方向に移動させる - pointer.shifting(); - } - } - - return brockTimes; - - } - - public static List calcurateTime(List brockTimes) { - // 読み取った値を全体の1/4, 2/4, 3/4, 4/4、ごとに和をとり、リストに格納 - List fourTimes = new ArrayList<>(); - fourTimes.add(brockTimes.subList(0, brockTimes.size() / 4).stream().reduce(Integer::sum).get()); - fourTimes.add(brockTimes.subList(0, brockTimes.size() / 2).stream().reduce(Integer::sum).get()); - fourTimes - .add(brockTimes.subList(0, brockTimes.size() * 3 / 4).stream().reduce(Integer::sum).get()); - fourTimes.add(brockTimes.subList(0, brockTimes.size()).stream().reduce(Integer::sum).get()); - return fourTimes; - } -} - - -/** - * 要素を読み取る方向 - */ -enum Direction { - RIGHT, - DOWN, - LEFT, - UP, -} - - -/** - * 現在読み取っている位置を示すポインターのクラス - * - */ -final class IndexPointer { - - private int rowIndex;// 現在の行の位置 - private int columnIndex;// 現在の列の位置 - private Direction direction; - - public IndexPointer(int rowIndex, int columnIndex, Direction direction) { - this.rowIndex = rowIndex; - this.columnIndex = columnIndex; - this.direction = direction; - } - - public Direction getDirection() { - return direction; - } - - public int getRowIndex() { - return rowIndex; - } - - public int getColumnIndex() { - return columnIndex; - } - - // -1の要素に移動した際、カーソルの位置を戻す処理 - // 時計周りに移動方向を変える - public void modifying() { - - switch (direction) { - case RIGHT: - this.columnIndex--; - this.rowIndex++; - direction = Direction.DOWN; - break; - case DOWN: - this.columnIndex--; - this.rowIndex--; - direction = Direction.LEFT; - break; - case LEFT: - this.columnIndex++; - this.rowIndex--; - direction = Direction.UP; - break; - case UP: - this.columnIndex++; - this.rowIndex++; - direction = Direction.RIGHT; - break; - } - } - - // カーソルを現在進む方向に移動させる - public void shifting() { - - switch (direction) { - case RIGHT: - this.columnIndex++; - break; - case DOWN: - this.rowIndex++; - break; - case LEFT: - this.columnIndex--; - break; - case UP: - this.rowIndex--; - break; - } - } -} diff --git a/kkanazawa/test/AlphabetAdditionTest.java b/kkanazawa/test/AlphabetAdditionTest.java new file mode 100644 index 0000000..6466089 --- /dev/null +++ b/kkanazawa/test/AlphabetAdditionTest.java @@ -0,0 +1,194 @@ +package paiza.src; + +import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.NoSuchElementException; +import org.junit.Before; +import org.junit.Test; + +// @RunWith(Enclosed.class) +public class AlphabetAdditionTest { + + public static class AlphabetIOTest { + AlphabetIO alphabetIO; + + @Before + public void setUp() { + alphabetIO = new AlphabetIO(); + + } + + String firstInput = "BEE"; + String secondInput = "CD"; + + @Test + public void BEEとCEが入力値のときinputValがBEEとCDが格納されたAdditionVal型のオブジェクトを返す() { + String input = "BEE CD"; + ByteArrayInputStream testInputStream = new ByteArrayInputStream(input + .getBytes(StandardCharsets.UTF_8)); + // System.in をテスト用のInputStreamに置き換える + System.setIn(testInputStream); + AdditionVal expect = new AdditionVal("BEE", "CD"); + AdditionVal actual = AlphabetIO.inputVal(); + assertEquals(expect.getFirstVal(), actual.getFirstVal()); + assertEquals(expect.getSecondVal(), actual.getSecondVal()); + } + + @Test(expected = NoSuchElementException.class) + public void 何も入力されないとき入力値がない時のエラーを返す() { + String input = ""; + ByteArrayInputStream testInputStream = new ByteArrayInputStream(input + .getBytes(StandardCharsets.UTF_8)); + // System.in をテスト用のInputStreamに置き換える + System.setIn(testInputStream); + AlphabetIO.inputVal(); + } + + @Test + public void 入力値の1番目が単一のAでないかつ一文字目はAのとき不正な値であることを表示() { + String input = "ABB CD"; + String expected = "不正な値です" + + System.lineSeparator(); + ByteArrayInputStream testInputStream = new ByteArrayInputStream(input + .getBytes(StandardCharsets.UTF_8)); + // System.in をテスト用のInputStreamに置き換える + System.setIn(testInputStream); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + AlphabetIO.inputVal(); + assertEquals(expected, out.toString()); + } + + @Test + public void 入力値の2番目が単一のAでないかつ一文字目はAのとき不正な値であることを表示() { + String input = "CBB AD"; + String expected = "不正な値です" + + System.lineSeparator(); + ByteArrayInputStream testInputStream = new ByteArrayInputStream(input + .getBytes(StandardCharsets.UTF_8)); + // System.in をテスト用のInputStreamに置き換える + System.setIn(testInputStream); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + AlphabetIO.inputVal(); + assertEquals(expected, out.toString()); + } + + @Test + public void 入力値の両方が単一のAでないかつ一文字目はAのとき不正な値であることを表示() { + String input = "ABB AD"; + String expected = "不正な値です" + + System.lineSeparator(); + ByteArrayInputStream testInputStream = new ByteArrayInputStream(input + .getBytes(StandardCharsets.UTF_8)); + // System.in をテスト用のInputStreamに置き換える + System.setIn(testInputStream); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + AlphabetIO.inputVal(); + assertEquals(expected, out.toString()); + } + + @Test + public void isIncorrectValueに単一のAを渡すとfalseを返す() { + String input = "A"; + boolean actual = AlphabetIO.isIncorrectValue(input); + assertEquals(false, actual); + } + + @Test + public void isIncorrectValueに一文字目がAではない文字列BBAを渡すとfalseを返す() { + String input = "BAA"; + boolean actual = AlphabetIO.isIncorrectValue(input); + assertEquals(false, actual); + } + + @Test + public void isIncorrectValueに一文字目がAである文字列ABBを渡すとtrueを返す() { + String input = "ABB"; + boolean actual = AlphabetIO.isIncorrectValue(input); + assertEquals(true, actual); + } + + } + + public static class AdditionValTest { + + AdditionVal testPair; + + @Before + public void setUp() { + testPair = new AdditionVal("BEE", "CD"); + + } + + @Test + public void BEEとCDがコンストラクタの引数のときalphabetSumがCCCを返す() { + + String expect = "CCC"; + assertEquals(expect, testPair.alphabetSum()); + + } + + } + + public static class AlphabetToNumTest { + AlphabetToNum alphabet; + + @Before + public void seUp() { + alphabet = new AlphabetToNum("BEE"); + } + + @Test + public void 引数がBEEの時alphabetToQuinaryは5進数に変換した結果144を返す() { + final int expect = 144; + assertEquals(expect, alphabet.alphabetToQuinary("BEE")); + } + + @Test + public void 引数がAの時alphabetToQuinaryは5進数に変換した結果0を返す() { + final int expect = 0; + assertEquals(expect, alphabet.alphabetToQuinary("A")); + } + + @Test + public void 引数が144の時alphabetToQuinaryは5進数に変換した結果49を返す() { + final int expect = 49; + assertEquals(expect, alphabet.quinaryToDecimal(144)); + } + + } + + public static class NumToAlphabetTest { + NumToAlphabet num; + + @Before + public void seUp() { + num = new NumToAlphabet(62); + } + + @Test + public void 引数が62の時decimalToQuinaryは5進数に変換した結果222を返す() { + final int expect = 222; + assertEquals(expect, num.decimalToQuinary(62)); + } + + @Test + public void 引数が222の時quinaryToAlphabetは5進数からアルファベットに変換した結果CCCを返す() { + final String expect = "CCC"; + assertEquals(expect, num.quinaryToAlphabet(222)); + } + + @Test + public void 引数が0の時quinaryToAlphabetは5進数からアルファベットに変換した結果Aを返す() { + final String expect = "A"; + assertEquals(expect, num.quinaryToAlphabet(0)); + } + + } + +} diff --git a/kkanazawa/test/KatoriTest.java b/kkanazawa/test/KatoriTest.java deleted file mode 100644 index 3e0c1cd..0000000 --- a/kkanazawa/test/KatoriTest.java +++ /dev/null @@ -1,204 +0,0 @@ -package paiza.src; - -import static org.junit.Assert.*; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.List; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.theories.DataPoints; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; - -public class KatoriTest { - - //二次元配列の周りを囲う値 - final static int BARRIER_NUM = -1; - //二次元配列の行数、列数 - final static int SIDE_LENGTH = 4; - - public static class KatoriIOTest { - - - @Test - public void OutputTimesに12_39_57_82_を格納したリストを渡すと各要素が改行を入れて出力される() { - String expected = 12 - + System.lineSeparator() - + - 39 - + System.lineSeparator() - + - 57 - + System.lineSeparator() - + - 82 - +System.lineSeparator(); - - List fourTimes = new ArrayList<>(); - fourTimes.add(12); - fourTimes.add(39); - fourTimes.add(57); - fourTimes.add(82); - final ByteArrayOutputStream out = new ByteArrayOutputStream(); - System.setOut(new PrintStream(out)); - KatoriIO.outputTimes(fourTimes); - - assertEquals(expected,out.toString()); - } - } - - public static class CalckatoriTest { - int[][] coil; - int[][] paddingCoil; - List decodedKatori; - List times; - - @Before - public void setUp() { - coil = new int[][] { - { - 3, 4, 3, 2 - }, { - 7, 9, 3, 7 - }, { - 1, 9, 4, 6 - }, { - 3, 7, 8, 6 - } - }; - - paddingCoil = new int[][] { - { - -1, -1, -1, -1, -1, -1 - }, { - -1, 3, 4, 3, 2, -1 - }, { - -1, 7, 9, 3, 7, -1 - }, { - -1, 1, 9, 4, 6, -1 - }, { - -1, 3, 7, 8, 6, -1 - }, { - -1, -1, -1, -1, -1, -1 - } - }; - - decodedKatori = new ArrayList<>(); - decodedKatori.add(3); - decodedKatori.add(4); - decodedKatori.add(3); - decodedKatori.add(2); - decodedKatori.add(7); - decodedKatori.add(6); - decodedKatori.add(6); - decodedKatori.add(8); - decodedKatori.add(7); - decodedKatori.add(3); - decodedKatori.add(1); - decodedKatori.add(7); - decodedKatori.add(9); - decodedKatori.add(3); - decodedKatori.add(4); - decodedKatori.add(9); - - times = new ArrayList<>(); - times.add(12); - times.add(39); - times.add(57); - times.add(82); - } - - @Test - public void PaddingCoilに4行4列の二次元配列coilを渡すとその二次元配列の周りをBARRIER_NUMで囲った二次元配列を返す() { - int[][] result = CalcKatori.paddingCoil(coil, SIDE_LENGTH); - for (int row = 0; row < SIDE_LENGTH + 2; row++) { - for (int column = 0; column < SIDE_LENGTH + 2; column++) { - assertEquals(paddingCoil[row][column], result[row][column]); - } - } - } - - @Test - public void decodedKatoriに二次元配列paddingCoilを渡すとうずまき状に要素を読み取ったリスト返す() { - List result = CalcKatori.decodeKatori(SIDE_LENGTH, paddingCoil); - assertEquals(decodedKatori, result); - } - - @Test - public void calcurateTimeに1次元リストdecodedKatoriを渡すと四分の一_四分の二_四分の三_四分の四ごとに和をとった値のリストを返す() { - List result = CalcKatori.calcurateTime(decodedKatori); - assertEquals(times, result); - } - } - - public static class IndexPointerTest { - - @Test - public void コンストラクタで設定した値がゲッターで取得できる() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); - assertEquals(1, pointer.getRowIndex()); - assertEquals(5, pointer.getColumnIndex()); - assertEquals(Direction.RIGHT, pointer.getDirection()); - - } - - @Test - public void modifyingでRIGHTから時計回りに1周してRIGHTに戻ってくる() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); - assertEquals(Direction.RIGHT, pointer.getDirection()); - pointer.modifying(); - assertEquals(Direction.DOWN, pointer.getDirection()); - pointer.modifying(); - assertEquals(Direction.LEFT, pointer.getDirection()); - pointer.modifying(); - assertEquals(Direction.UP, pointer.getDirection()); - pointer.modifying(); - assertEquals(Direction.RIGHT, pointer.getDirection()); - - } - - @RunWith(Theories.class) - public static class shiftingのテスト { - @DataPoints - public static Fixture[] PARAMS = { - new Fixture(1, 5, Direction.RIGHT, 1, 6), - new Fixture(1, 5, Direction.DOWN, 2, 5), - new Fixture(1, 5, Direction.LEFT, 1, 4), - new Fixture(1, 5, Direction.UP, 0, 5), - }; - @Theory - //RIGHT:行インデックス+0 列インデックス+1 - //DOWN:行インデックス+1 列インデックス+0 - //LEFT:行インデックス+0 列インデックスー1 - //UP:行インデックスー1 列インデックス+0 - public void RIGHT_DOWN_LEFT_UPに対してそれぞれインデックスが適切に変動(Fixture params) throws Exception { - IndexPointer pointer = new IndexPointer(params.indexRow, params.indexColumn, params.direction); - pointer.shifting(); - assertEquals(params.expectedRow, pointer.getRowIndex()); - assertEquals(params.expectedColumn, pointer.getColumnIndex()); - } - - //パラメータ化用のクラス - static class Fixture { - int indexRow; - int indexColumn; - Direction direction; - int expectedRow; - int expectedColumn; - - Fixture(int indexRow, int indexColumn, Direction direction, int expectedRow, int expectedColumn) { - this.indexRow = indexRow; - this.indexColumn = indexColumn; - this.direction = direction; - this.expectedRow = expectedRow; - this.expectedColumn = expectedColumn; - } - } - - } - - - } -} -- GitLab From 2c3579e5a2f0159740c80d09810db4ddb86a1c82 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Fri, 25 Jul 2025 14:23:05 +0900 Subject: [PATCH 10/11] =?UTF-8?q?painza=E3=81=AE=E5=95=8F=E9=A1=8C?= =?UTF-8?q?=E3=81=AEB054=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/AlphabetAddition.java | 46 ++++++++++++++--------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/kkanazawa/src/AlphabetAddition.java b/kkanazawa/src/AlphabetAddition.java index b9e9e14..9f37c6f 100644 --- a/kkanazawa/src/AlphabetAddition.java +++ b/kkanazawa/src/AlphabetAddition.java @@ -36,25 +36,25 @@ final class AlphabetIO { firstVal = scanner.next(); secondVal = scanner.next(); } catch (NoSuchElementException e) { - throw new NoSuchElementException("エラー:入力値がありません"); + throw new NoSuchElementException( "エラー:入力値がありません"); + } + // 入力値が単一の"A"でないなら、一文字目は"A"でないことを確認 if (isIncorrectValue(firstVal) || isIncorrectValue(secondVal)) { System.out.println("不正な値です"); } final AdditionVal additionNum = new AdditionVal(firstVal, secondVal); - return additionNum; } // 入力値が適切な値か判定するメソッド // 適切な値は「入力値が単一の"A"でないなら、一文字目は"A"でない」を満たす - public static boolean isIncorrectValue(String val) { + public static boolean isIncorrectValue(final String val) { if (val.equals("A")) { return false; } else { - return val.split("")[0].equals("A"); } } @@ -67,8 +67,8 @@ final class AlphabetIO { * */ final class AdditionVal { - private String firstVal; - private String secondVal; + private final String firstVal; + private final String secondVal; public AdditionVal(final String firstNum, final String secondNum) { this.firstVal = new String(firstNum); @@ -89,7 +89,7 @@ final class AdditionVal { AlphabetToNum secondFix = new AlphabetToNum(secondVal); // 10進数に変換した値の和をとる - final int decimalSum = firstFix.getDecimalNum() + secondFix.getDecimalNum(); + final long decimalSum = firstFix.getDecimalNum() + secondFix.getDecimalNum(); // 10進数の和の値をアルファベットに変換して返す return new NumToAlphabet(decimalSum).getAlphabetVal(); @@ -117,8 +117,8 @@ final class AlphabetToNum { // 5進数を10進数に変換する際に使用する基数 static final int FUND_NUM = 5; private final String AlphabetVal; - private final int quinaryNum; - private final int decimalNum; + private final long quinaryNum; + private final long decimalNum; public AlphabetToNum(final String AlphabetNum) { this.AlphabetVal = AlphabetNum; @@ -126,29 +126,29 @@ final class AlphabetToNum { this.decimalNum = quinaryToDecimal(quinaryNum); } - public int getDecimalNum() { + public long getDecimalNum() { return decimalNum; } // アルファベットを5進数に変換して返す - public int alphabetToQuinary(String AlphabetVal) { - int quinaryNum = 0; + public long alphabetToQuinary(String AlphabetVal) { + long quinaryNum = 0; String[] firstSplit = AlphabetVal.split(""); // 桁数のカウント - int digitsCount = firstSplit.length - 1; + long digitsCount = firstSplit.length - 1; // アルファベットに対応する5進数を桁の大きい方から順に変換 for (String str : firstSplit) { - quinaryNum += quinaryFix.get(str) * (int) Math.pow(10, digitsCount); + quinaryNum += quinaryFix.get(str) * (long) Math.pow(10, digitsCount); digitsCount--; } return quinaryNum; } // 5進数から10進数に変換して返す - public int quinaryToDecimal(int quinaryNum) { + public long quinaryToDecimal(long quinaryNum) { String numStr = String.valueOf(quinaryNum); - int decimalNum = Integer.parseInt(numStr, FUND_NUM); + long decimalNum = Long.parseLong(numStr, FUND_NUM); return decimalNum; } @@ -174,11 +174,11 @@ final class NumToAlphabet { // 10進数を5進数に変換する際に使用する基数 static final int FUND_NUM = 5; - private final int decimalNum; - private final int quinaryNum; + private final long decimalNum; + private final long quinaryNum; private final String AlphabetVal; - public NumToAlphabet(final int decimalNum) { + public NumToAlphabet(final long decimalNum) { this.decimalNum = decimalNum; this.quinaryNum = decimalToQuinary(this.decimalNum); this.AlphabetVal = quinaryToAlphabet(quinaryNum); @@ -189,14 +189,14 @@ final class NumToAlphabet { } // 10進数を5進数に変換して返す - public int decimalToQuinary(int decinalNum) { - int quinaryNum = Integer.parseInt(Integer.toString(decimalNum, FUND_NUM)); + public long decimalToQuinary(long decinalNum) { + long quinaryNum = Long.parseLong(Long.toString(decimalNum, FUND_NUM)); return quinaryNum; } // 5進数をアルファベットに変換して返す - public String quinaryToAlphabet(int quinaryNum) { + public String quinaryToAlphabet(long quinaryNum) { String numStr = String.valueOf(quinaryNum); List splitAlphabet = new ArrayList<>(); @@ -207,7 +207,7 @@ final class NumToAlphabet { } // 変換したアルファベットのリストを文字列に結合 String AlphabetVal = String.join("", splitAlphabet); - return new String(AlphabetVal); + return AlphabetVal; } } -- GitLab From a400f2e43ef6b73763b0f80c71f76df8d6cf3051 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 28 Jul 2025 16:07:00 +0900 Subject: [PATCH 11/11] =?UTF-8?q?Paiza=E3=81=AE=E5=95=8F=E9=A1=8C=E3=81=AE?= =?UTF-8?q?B054=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/AlphabetAddition.java | 67 +++++++++++++----------- kkanazawa/test/AlphabetAdditionTest.java | 10 ++-- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/kkanazawa/src/AlphabetAddition.java b/kkanazawa/src/AlphabetAddition.java index 9f37c6f..b9b2158 100644 --- a/kkanazawa/src/AlphabetAddition.java +++ b/kkanazawa/src/AlphabetAddition.java @@ -12,14 +12,15 @@ public class AlphabetAddition { public static void main(final String[] args) { // 入力処理 - final AdditionVal alphabetPair = AlphabetIO.inputVal(); + final AlphabetPair alphabetPair = AlphabetIO.inputVal(); // 入力値の和の計算&計算結果の出力処理 - System.out.println(alphabetPair.alphabetSum()); + System.out.println(alphabetPair.calcTotal()); } } + /** * 入力処理、出力処理を行うクラス * @@ -27,7 +28,7 @@ public class AlphabetAddition { final class AlphabetIO { // 二つのアルファベットの値を入力し、それらをまとめるadditioValクラスとして返す - public static AdditionVal inputVal() { + public static AlphabetPair inputVal() { String firstVal = new String(); String secondVal = new String(); @@ -36,16 +37,16 @@ final class AlphabetIO { firstVal = scanner.next(); secondVal = scanner.next(); } catch (NoSuchElementException e) { - throw new NoSuchElementException( "エラー:入力値がありません"); - + throw new NoSuchElementException("エラー:入力値がありません"); + } - + // 入力値が単一の"A"でないなら、一文字目は"A"でないことを確認 if (isIncorrectValue(firstVal) || isIncorrectValue(secondVal)) { System.out.println("不正な値です"); } - final AdditionVal additionNum = new AdditionVal(firstVal, secondVal); + final AlphabetPair additionNum = new AlphabetPair(firstVal, secondVal); return additionNum; } @@ -66,11 +67,11 @@ final class AlphabetIO { * アルファベットのペアの和をとるクラス * */ -final class AdditionVal { +final class AlphabetPair { private final String firstVal; private final String secondVal; - public AdditionVal(final String firstNum, final String secondNum) { + public AlphabetPair(final String firstNum, final String secondNum) { this.firstVal = new String(firstNum); this.secondVal = new String(secondNum); } @@ -84,7 +85,7 @@ final class AdditionVal { } // 二つのアルファベットの値を一度10進数に変換して和をとった後、再びアルファベットに直して返す - public String alphabetSum() { + public String calcTotal() { AlphabetToNum firstFix = new AlphabetToNum(firstVal); AlphabetToNum secondFix = new AlphabetToNum(secondVal); @@ -98,13 +99,9 @@ final class AdditionVal { } -/** - * アルファベットから10進数に変換するクラス - * - */ -final class AlphabetToNum { - // アルファベットと5進数の対応 - private final static Map quinaryFix = new HashMap<>() { +final class FixMap { + // アルファベットから5進数への対応 + public final static Map quinaryFix = new HashMap<>() { { put("A", 0); put("B", 1); @@ -114,6 +111,25 @@ final class AlphabetToNum { } }; + // 5進数からアルファベットへの対応 + public final static Map AlphabetFix = new HashMap<>() { + { + put("0", "A"); + put("1", "B"); + put("2", "C"); + put("3", "D"); + put("4", "E"); + } + }; +} + + +/** + * アルファベットから10進数に変換するクラス + * + */ +final class AlphabetToNum { + // 5進数を10進数に変換する際に使用する基数 static final int FUND_NUM = 5; private final String AlphabetVal; @@ -139,7 +155,7 @@ final class AlphabetToNum { // アルファベットに対応する5進数を桁の大きい方から順に変換 for (String str : firstSplit) { - quinaryNum += quinaryFix.get(str) * (long) Math.pow(10, digitsCount); + quinaryNum += FixMap.quinaryFix.get(str) * (long) Math.pow(10, digitsCount); digitsCount--; } return quinaryNum; @@ -161,17 +177,6 @@ final class AlphabetToNum { */ final class NumToAlphabet { - // 5進数とアルファベットの対応 - private final static Map AlphabetFix = new HashMap<>() { - { - put("0", "A"); - put("1", "B"); - put("2", "C"); - put("3", "D"); - put("4", "E"); - } - }; - // 10進数を5進数に変換する際に使用する基数 static final int FUND_NUM = 5; private final long decimalNum; @@ -185,7 +190,7 @@ final class NumToAlphabet { } public String getAlphabetVal() { - return AlphabetVal ; + return AlphabetVal; } // 10進数を5進数に変換して返す @@ -203,7 +208,7 @@ final class NumToAlphabet { // 文字列型にした5進数をsplit()で1つずつに分け、順番にアルファベットに変換 for (String str : numStr.split("")) { - splitAlphabet.add(AlphabetFix.get(str)); + splitAlphabet.add(FixMap.AlphabetFix.get(str)); } // 変換したアルファベットのリストを文字列に結合 String AlphabetVal = String.join("", splitAlphabet); diff --git a/kkanazawa/test/AlphabetAdditionTest.java b/kkanazawa/test/AlphabetAdditionTest.java index 6466089..3d9bca1 100644 --- a/kkanazawa/test/AlphabetAdditionTest.java +++ b/kkanazawa/test/AlphabetAdditionTest.java @@ -31,8 +31,8 @@ public class AlphabetAdditionTest { .getBytes(StandardCharsets.UTF_8)); // System.in をテスト用のInputStreamに置き換える System.setIn(testInputStream); - AdditionVal expect = new AdditionVal("BEE", "CD"); - AdditionVal actual = AlphabetIO.inputVal(); + AlphabetPair expect = new AlphabetPair("BEE", "CD"); + AlphabetPair actual = AlphabetIO.inputVal(); assertEquals(expect.getFirstVal(), actual.getFirstVal()); assertEquals(expect.getSecondVal(), actual.getSecondVal()); } @@ -117,11 +117,11 @@ public class AlphabetAdditionTest { public static class AdditionValTest { - AdditionVal testPair; + AlphabetPair testPair; @Before public void setUp() { - testPair = new AdditionVal("BEE", "CD"); + testPair = new AlphabetPair("BEE", "CD"); } @@ -129,7 +129,7 @@ public class AlphabetAdditionTest { public void BEEとCDがコンストラクタの引数のときalphabetSumがCCCを返す() { String expect = "CCC"; - assertEquals(expect, testPair.alphabetSum()); + assertEquals(expect, testPair.calcTotal()); } -- GitLab