From 414f57f8f04307fe57d5aba3a9b8459a2143dfaa Mon Sep 17 00:00:00 2001 From: rooki Date: Tue, 22 Jul 2025 17:43:28 +0900 Subject: [PATCH 1/5] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB129=E3=81=AE?= =?UTF-8?q?=E5=9B=9E=E7=AD=94=E3=80=81=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B129.java | 147 ++++++++++++++++++++++++++++++++++++++++++++ rooki/B129Test.java | 92 +++++++++++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 rooki/B129.java create mode 100644 rooki/B129Test.java diff --git a/rooki/B129.java b/rooki/B129.java new file mode 100644 index 0000000..9ef4b7c --- /dev/null +++ b/rooki/B129.java @@ -0,0 +1,147 @@ +import java.util.Scanner; + +/** + * 農場作業が終わった後の収穫個数と作物状況を出力. + * + * @author rooki + * @version 1.0 + */ +public class B129 { + + /** + * mainメソッド. + * + * @param args 使用しない + */ + public static void main(final String[] args) { + final Scanner sc = new Scanner(System.in); + + // 農場の情報を保存 + final FarmInfo farmer = new FarmInfo(sc); + + harvestCrops(farmer); + + final String result = outputResult(farmer); + + System.out.print(result); + + sc.close(); + } + + /** + * 収穫作業を行う. + * + * @param farmer 農場の情報を持つ + */ + public static void harvestCrops(FarmInfo farmer) { + // 作業回数 + for (int times = 0; times < farmer.workingTimes; times++) { + // 何行から何行まで植えるか + for (int line = + farmer.workingRange[times][0]; line <= farmer.workingRange[times][1]; line++) { + // 何列から何列まで植えるか + for (int column = + farmer.workingRange[times][2]; column <= farmer.workingRange[times][3]; column++) { + // 作物を収穫し、数える + for (int i = 1; i <= farmer.categoryNumber; i++) { + // もし作物があれば収穫する + if (i == farmer.farmMap[line - 1][column - 1]) { + // 配列のインデックスと対応している箇所の作物の個数を増やす + farmer.crops[i - 1]++; + } + } + // 指定範囲に指定作物を植える + farmer.farmMap[line - 1][column - 1] = farmer.workingRange[times][4]; + } + } + } + } + + /** + * 収穫できた個数、作業後の作物の情報を出力する. + * + * @param farmer 農場の情報を持つ + */ + public static String outputResult(final FarmInfo farmer) { + String result = ""; + for (int cropsType = 0; cropsType < farmer.categoryNumber; cropsType++) { + result += (farmer.crops[cropsType]) + + "\r\n"; + } + + for (int[] lineRange : farmer.farmMap) { + for (int farmStatus : lineRange) { + if (farmStatus == 0) { + result += ("."); + } else { + result += (farmStatus); + } + } + result += "\r\n"; + } + return result; + } + + /** + * 農場の情報を持つクラス. + * + */ + + static class FarmInfo { + private int workingTimes; + private int categoryNumber; + private int lineCount; + private int columnCount; + private int[][] workingRange; + private int[][] farmMap; + private int[] crops; + + public FarmInfo(final Scanner sc) { + this.workingTimes = sc.nextInt(); // 1年の作業回数 + this.categoryNumber = sc.nextInt(); // 作物の種類数 + this.lineCount = sc.nextInt(); // 行数 + this.columnCount = sc.nextInt(); // 列数 + + this.farmMap = new int[lineCount][columnCount]; // 指定したサイズの農場 + this.crops = new int[categoryNumber]; // 収穫した作物の個数を保存する配列 + + this.workingRange = new int[workingTimes][5]; + for (int times = 0; times < workingTimes; times++) { + for (int j = 0; j < 5; j++) { + this.workingRange[times][j] = sc.nextInt(); // 作業範囲と植える作物の種類を保存 + } + } + + } + + public FarmInfo() {} + + public int[][] getFarmMap() { + return farmMap; + } + + public void setFarmMap(int[][] farmMap) { + this.farmMap = farmMap; + } + + public void setWorkingTimes(int workingTimes) { + this.workingTimes = workingTimes; + } + + public void setWorkingRange(int[][] workingRange) { + this.workingRange = workingRange; + } + + public int[] getCrops() { + return crops; + } + + public void setCrops(int[] crops) { + this.crops = crops; + } + + public void setCategoryNumber(int categoryNumber) { + this.categoryNumber = categoryNumber; + } + } +} diff --git a/rooki/B129Test.java b/rooki/B129Test.java new file mode 100644 index 0000000..db4568d --- /dev/null +++ b/rooki/B129Test.java @@ -0,0 +1,92 @@ +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +/** + * B129のテストコード. + * + * @author rooki + * @version 1.0 + */ +public class B129Test { + /** + * harvestCropsメソッドの計算が正しいかテスト. + * + */ + public static class harvestCropsのテスト { + B129.FarmInfo farmer = new B129.FarmInfo(); + + @Before + public void setup() { + farmer.setWorkingTimes(3); + farmer.setCategoryNumber(2); + farmer.setWorkingRange(new int[][] { + { + 1, 2, 3, 4, 1 + }, + { + 1, 1, 1, 4, 2 + }, + { + 1, 2, 2, 3, 1 + } + }); + farmer.setFarmMap(new int[2][4]); // 3x3の空の農場 + farmer.setCrops(new int[2]); + + B129.harvestCrops(farmer); + } + + @Test + public void 収穫数が一致しているかテスト() throws Exception { + int[] expectedCrops = { + 3, 2 + }; + assertArrayEquals(expectedCrops, farmer.getCrops()); + } + + @Test + public void 農場の最終状態が一致しているかテスト() throws Exception { + int[][] expectedFarmMap = { + { + 2, 1, 1, 2 + }, { + 0, 1, 1, 1 + } + }; + assertArrayEquals(expectedFarmMap, farmer.getFarmMap()); + } + } + + /** + * outputResultが与えられたデータを正しく出力しているかテスト. + * + */ + public static class outputResultのテスト { + B129.FarmInfo farmer = new B129.FarmInfo(); + + @Before + public void setup() { + farmer.setCategoryNumber(2); + farmer.setCrops(new int[] { + 3, 2 + }); // 作物1が3個, 作物2が2個 + farmer.setFarmMap(new int[][] { + { + 2, 1, 1, 2 + }, + { + 0, 1, 1, 1 + } + }); + + B129.outputResult(farmer); + } + + @Test + public void データの最終出力が一致しているかテスト() throws Exception { + String expectedOutput = "3\r\n2\r\n2112\r\n.111\r\n"; + assertEquals(expectedOutput, B129.outputResult(farmer)); + } + } +} -- GitLab From 7a31917b88b37e3e45ad36c4a7eb3b0ddc6f998f Mon Sep 17 00:00:00 2001 From: rooki Date: Wed, 23 Jul 2025 17:42:44 +0900 Subject: [PATCH 2/5] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB129=E3=81=AE?= =?UTF-8?q?=E5=9B=9E=E7=AD=94=E3=80=81=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E5=AF=BE=E3=81=97=E3=81=A6=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B129.java | 151 ++++++++++++++++++++++---------------------- rooki/B129Test.java | 146 +++++++++++++++++++++--------------------- 2 files changed, 150 insertions(+), 147 deletions(-) diff --git a/rooki/B129.java b/rooki/B129.java index 9ef4b7c..eeaa803 100644 --- a/rooki/B129.java +++ b/rooki/B129.java @@ -4,7 +4,7 @@ import java.util.Scanner; * 農場作業が終わった後の収穫個数と作物状況を出力. * * @author rooki - * @version 1.0 + * @version 2.0 */ public class B129 { @@ -14,18 +14,13 @@ public class B129 { * @param args 使用しない */ public static void main(final String[] args) { - final Scanner sc = new Scanner(System.in); - - // 農場の情報を保存 - final FarmInfo farmer = new FarmInfo(sc); + final FarmInfo farmer = FarmInfo.createFrom(new Scanner(System.in)); harvestCrops(farmer); - final String result = outputResult(farmer); - - System.out.print(result); + final StringBuilder result = outputResult(farmer); - sc.close(); + System.out.print(result); } /** @@ -33,7 +28,7 @@ public class B129 { * * @param farmer 農場の情報を持つ */ - public static void harvestCrops(FarmInfo farmer) { + public static void harvestCrops(final FarmInfo farmer) { // 作業回数 for (int times = 0; times < farmer.workingTimes; times++) { // 何行から何行まで植えるか @@ -42,42 +37,56 @@ public class B129 { // 何列から何列まで植えるか for (int column = farmer.workingRange[times][2]; column <= farmer.workingRange[times][3]; column++) { - // 作物を収穫し、数える - for (int i = 1; i <= farmer.categoryNumber; i++) { - // もし作物があれば収穫する - if (i == farmer.farmMap[line - 1][column - 1]) { - // 配列のインデックスと対応している箇所の作物の個数を増やす - farmer.crops[i - 1]++; - } - } + + countCrops(farmer, line, column); // 指定範囲に指定作物を植える - farmer.farmMap[line - 1][column - 1] = farmer.workingRange[times][4]; + farmer.farmMap[line - 1][column - 1] = farmer.plantCrops[times]; } } } } + /** + * 作物の数を数える. + * + * @param farmer 農場の情報を持つ + * @param line 行数 + * @param column 列数 + */ + public static void countCrops(final FarmInfo farmer, int line, int column) { + // 作物を収穫し、数える + for (int i = 1; i <= farmer.categoryNumber; i++) { + // もし作物があれば収穫する + if (i == farmer.farmMap[line - 1][column - 1]) { + // 配列のインデックスと対応している箇所の作物の個数を増やす + farmer.crops[i - 1]++; + } + } + } + /** * 収穫できた個数、作業後の作物の情報を出力する. * * @param farmer 農場の情報を持つ */ - public static String outputResult(final FarmInfo farmer) { - String result = ""; + public static StringBuilder outputResult(final FarmInfo farmer) { + StringBuilder result = new StringBuilder(); + for (int cropsType = 0; cropsType < farmer.categoryNumber; cropsType++) { - result += (farmer.crops[cropsType]) - + "\r\n"; + result + .append(farmer.crops[cropsType] + + "\r\n"); } for (int[] lineRange : farmer.farmMap) { for (int farmStatus : lineRange) { if (farmStatus == 0) { - result += ("."); + result.append("."); } else { - result += (farmStatus); + result.append(farmStatus); } } - result += "\r\n"; + result.append("\r\n"); } return result; } @@ -86,62 +95,56 @@ public class B129 { * 農場の情報を持つクラス. * */ - static class FarmInfo { - private int workingTimes; - private int categoryNumber; - private int lineCount; - private int columnCount; - private int[][] workingRange; - private int[][] farmMap; - private int[] crops; - - public FarmInfo(final Scanner sc) { - this.workingTimes = sc.nextInt(); // 1年の作業回数 - this.categoryNumber = sc.nextInt(); // 作物の種類数 - this.lineCount = sc.nextInt(); // 行数 - this.columnCount = sc.nextInt(); // 列数 - - this.farmMap = new int[lineCount][columnCount]; // 指定したサイズの農場 - this.crops = new int[categoryNumber]; // 収穫した作物の個数を保存する配列 - - this.workingRange = new int[workingTimes][5]; - for (int times = 0; times < workingTimes; times++) { - for (int j = 0; j < 5; j++) { - this.workingRange[times][j] = sc.nextInt(); // 作業範囲と植える作物の種類を保存 - } - } - - } - - public FarmInfo() {} - - public int[][] getFarmMap() { - return farmMap; - } - - public void setFarmMap(int[][] farmMap) { + final int workingTimes; + final int categoryNumber; + final int[][] workingRange; + final int[] plantCrops; + final int[][] farmMap; + final int[] crops; + + /** + * テストコードで呼び出す際に利用するコンストラクタ. + */ + public FarmInfo(int workingTimes, int categoryNumber, int[][] workingRange, int[] plantCrops, + int[][] farmMap, + int[] crops) { + this.workingTimes = workingTimes; + this.categoryNumber = categoryNumber; + this.workingRange = workingRange; + this.plantCrops = plantCrops; this.farmMap = farmMap; + this.crops = crops; } - public void setWorkingTimes(int workingTimes) { - this.workingTimes = workingTimes; - } + /** + * Scannerからデータを読み込み、インスタンスを生成して返す. + * + * @param sc 読みこんだScanner + * @return 生成されたFarmInfoインスタンス + */ + public static FarmInfo createFrom(final Scanner sc) { + int workingTimes = sc.nextInt(); // 1年の作業回数 + int categoryNumber = sc.nextInt(); // 作物の種類数 + int lineCount = sc.nextInt(); // 行数 + int columnCount = sc.nextInt(); // 列数 - public void setWorkingRange(int[][] workingRange) { - this.workingRange = workingRange; - } + int[][] farmMap = new int[lineCount][columnCount]; // 指定したサイズの農場 + int[] crops = new int[categoryNumber]; // 収穫した作物の個数を保存する配列 - public int[] getCrops() { - return crops; - } + int[] plantCrops = new int[workingTimes]; - public void setCrops(int[] crops) { - this.crops = crops; - } + int[][] workingRange = new int[workingTimes][4]; + for (int times = 0; times < workingTimes; times++) { + for (int j = 0; j < 4; j++) { + workingRange[times][j] = sc.nextInt(); // 作業範囲を保存 + } + plantCrops[times] = sc.nextInt(); // 植える作物の種類を保存 + } + sc.close(); - public void setCategoryNumber(int categoryNumber) { - this.categoryNumber = categoryNumber; + // 読み込んだデータを使って、新しいインスタンスを生成して返す + return new FarmInfo(workingTimes, categoryNumber, workingRange, plantCrops, farmMap, crops); } } } diff --git a/rooki/B129Test.java b/rooki/B129Test.java index db4568d..0c1d6d9 100644 --- a/rooki/B129Test.java +++ b/rooki/B129Test.java @@ -1,92 +1,92 @@ import static org.junit.Assert.*; -import org.junit.Before; import org.junit.Test; /** * B129のテストコード. * * @author rooki - * @version 1.0 + * @version 2.0 */ public class B129Test { - /** - * harvestCropsメソッドの計算が正しいかテスト. - * - */ - public static class harvestCropsのテスト { - B129.FarmInfo farmer = new B129.FarmInfo(); - @Before - public void setup() { - farmer.setWorkingTimes(3); - farmer.setCategoryNumber(2); - farmer.setWorkingRange(new int[][] { - { - 1, 2, 3, 4, 1 - }, - { - 1, 1, 1, 4, 2 - }, - { - 1, 2, 2, 3, 1 - } - }); - farmer.setFarmMap(new int[2][4]); // 3x3の空の農場 - farmer.setCrops(new int[2]); + @Test + public void 植えられていない箇所に1を植えた際に最終状態が一致しているか() throws Exception { + int workingTimes = 1; + int categoryNumber = 2; + int[][] workingRange = { + { + 1, 1, 1, 1 + } + }; + int[] plantCrops = { + 1 + }; + int[][] farmMap = new int[1][4]; + int[] crops = new int[2]; - B129.harvestCrops(farmer); - } + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, workingRange, plantCrops, + farmMap, crops); - @Test - public void 収穫数が一致しているかテスト() throws Exception { - int[] expectedCrops = { - 3, 2 - }; - assertArrayEquals(expectedCrops, farmer.getCrops()); - } + B129.harvestCrops(farmer); - @Test - public void 農場の最終状態が一致しているかテスト() throws Exception { - int[][] expectedFarmMap = { - { - 2, 1, 1, 2 - }, { - 0, 1, 1, 1 - } - }; - assertArrayEquals(expectedFarmMap, farmer.getFarmMap()); - } + assertArrayEquals(new int[][] { + { + 1, 0, 0, 0 + } + }, farmer.farmMap); } - /** - * outputResultが与えられたデータを正しく出力しているかテスト. - * - */ - public static class outputResultのテスト { - B129.FarmInfo farmer = new B129.FarmInfo(); + @Test + public void 作物1を1つ収穫した際に収穫数が一致しているか() throws Exception { + int workingTimes = 2; + int categoryNumber = 2; + int[][] workingRange = { + { + 1, 1, 1, 1 + }, { + 1, 1, 1, 1 + } + }; + int[] plantCrops = { + 1, 1 + }; + int[][] farmMap = new int[1][4]; + int[] crops = new int[2]; - @Before - public void setup() { - farmer.setCategoryNumber(2); - farmer.setCrops(new int[] { - 3, 2 - }); // 作物1が3個, 作物2が2個 - farmer.setFarmMap(new int[][] { - { - 2, 1, 1, 2 - }, - { - 0, 1, 1, 1 - } - }); + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, workingRange, plantCrops, + farmMap, crops); - B129.outputResult(farmer); - } + B129.harvestCrops(farmer); - @Test - public void データの最終出力が一致しているかテスト() throws Exception { - String expectedOutput = "3\r\n2\r\n2112\r\n.111\r\n"; - assertEquals(expectedOutput, B129.outputResult(farmer)); - } + assertArrayEquals(new int[] { + 1, 0 + }, farmer.crops); } -} + + @Test + public void outputResultが正しい文字列を生成するか() throws Exception { + int workingTimes = 0; + int categoryNumber = 2; + int[][] farmMap = { + { + 1, 2 + }, { + 0, 0 + } + }; + int[] crops = { + 10, 5 + }; + + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, null, null, farmMap, + crops); + + StringBuilder actualResult = B129.outputResult(farmer); + + String expectedString = "10\r\n" + + "5\r\n" + + "12\r\n" + + "..\r\n"; + assertEquals(expectedString, actualResult.toString()); + } +} \ No newline at end of file -- GitLab From 9f155285739eb7d7828de7109e6ddcdefb5d0210 Mon Sep 17 00:00:00 2001 From: rooki Date: Thu, 24 Jul 2025 14:35:31 +0900 Subject: [PATCH 3/5] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB129=E3=81=AE?= =?UTF-8?q?=E5=9B=9E=E7=AD=94=E3=80=81=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E5=AF=BE=E3=81=97=E3=81=A6=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B129.java | 31 +++++++++++++++++++------------ rooki/B129Test.java | 35 +++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/rooki/B129.java b/rooki/B129.java index eeaa803..4ecdfcc 100644 --- a/rooki/B129.java +++ b/rooki/B129.java @@ -4,7 +4,7 @@ import java.util.Scanner; * 農場作業が終わった後の収穫個数と作物状況を出力. * * @author rooki - * @version 2.0 + * @version 3.0 */ public class B129 { @@ -33,10 +33,10 @@ public class B129 { for (int times = 0; times < farmer.workingTimes; times++) { // 何行から何行まで植えるか for (int line = - farmer.workingRange[times][0]; line <= farmer.workingRange[times][1]; line++) { + farmer.lineWorkingRange[times][0]; line <= farmer.lineWorkingRange[times][1]; line++) { // 何列から何列まで植えるか for (int column = - farmer.workingRange[times][2]; column <= farmer.workingRange[times][3]; column++) { + farmer.columnWorkingRange[times][0]; column <= farmer.columnWorkingRange[times][1]; column++) { countCrops(farmer, line, column); // 指定範囲に指定作物を植える @@ -70,7 +70,7 @@ public class B129 { * @param farmer 農場の情報を持つ */ public static StringBuilder outputResult(final FarmInfo farmer) { - StringBuilder result = new StringBuilder(); + final StringBuilder result = new StringBuilder(); for (int cropsType = 0; cropsType < farmer.categoryNumber; cropsType++) { result @@ -98,7 +98,8 @@ public class B129 { static class FarmInfo { final int workingTimes; final int categoryNumber; - final int[][] workingRange; + final int[][] lineWorkingRange; + final int[][] columnWorkingRange; final int[] plantCrops; final int[][] farmMap; final int[] crops; @@ -106,12 +107,14 @@ public class B129 { /** * テストコードで呼び出す際に利用するコンストラクタ. */ - public FarmInfo(int workingTimes, int categoryNumber, int[][] workingRange, int[] plantCrops, + public FarmInfo(int workingTimes, int categoryNumber, int[][] lineWorkingRange, + int[][] columnWorkingRange, int[] plantCrops, int[][] farmMap, int[] crops) { this.workingTimes = workingTimes; this.categoryNumber = categoryNumber; - this.workingRange = workingRange; + this.lineWorkingRange = lineWorkingRange; + this.columnWorkingRange = columnWorkingRange; this.plantCrops = plantCrops; this.farmMap = farmMap; this.crops = crops; @@ -134,17 +137,21 @@ public class B129 { int[] plantCrops = new int[workingTimes]; - int[][] workingRange = new int[workingTimes][4]; + int[][] lineWorkingRange = new int[workingTimes][2]; + int[][] columnWorkingRange = new int[workingTimes][2]; + for (int times = 0; times < workingTimes; times++) { - for (int j = 0; j < 4; j++) { - workingRange[times][j] = sc.nextInt(); // 作業範囲を保存 - } + lineWorkingRange[times][0] = sc.nextInt(); // 作業範囲を保存 + lineWorkingRange[times][1] = sc.nextInt(); + columnWorkingRange[times][0] = sc.nextInt(); + columnWorkingRange[times][1] = sc.nextInt(); plantCrops[times] = sc.nextInt(); // 植える作物の種類を保存 } sc.close(); // 読み込んだデータを使って、新しいインスタンスを生成して返す - return new FarmInfo(workingTimes, categoryNumber, workingRange, plantCrops, farmMap, crops); + return new FarmInfo(workingTimes, categoryNumber, lineWorkingRange, columnWorkingRange, + plantCrops, farmMap, crops); } } } diff --git a/rooki/B129Test.java b/rooki/B129Test.java index 0c1d6d9..6b562ff 100644 --- a/rooki/B129Test.java +++ b/rooki/B129Test.java @@ -5,7 +5,7 @@ import org.junit.Test; * B129のテストコード. * * @author rooki - * @version 2.0 + * @version 3.0 */ public class B129Test { @@ -13,9 +13,14 @@ public class B129Test { public void 植えられていない箇所に1を植えた際に最終状態が一致しているか() throws Exception { int workingTimes = 1; int categoryNumber = 2; - int[][] workingRange = { + int[][] lineWorkingRange = { { - 1, 1, 1, 1 + 1, 1 + } + }; + int[][] columnWorkingRange = { + { + 1, 1 } }; int[] plantCrops = { @@ -24,7 +29,8 @@ public class B129Test { int[][] farmMap = new int[1][4]; int[] crops = new int[2]; - B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, workingRange, plantCrops, + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, lineWorkingRange, + columnWorkingRange, plantCrops, farmMap, crops); B129.harvestCrops(farmer); @@ -40,11 +46,18 @@ public class B129Test { public void 作物1を1つ収穫した際に収穫数が一致しているか() throws Exception { int workingTimes = 2; int categoryNumber = 2; - int[][] workingRange = { + int[][] lineWorkingRange = { + { + 1, 1 + }, { + 1, 1 + } + }; + int[][] columnWorkingRange = { { - 1, 1, 1, 1 + 1, 1 }, { - 1, 1, 1, 1 + 1, 1 } }; int[] plantCrops = { @@ -53,7 +66,8 @@ public class B129Test { int[][] farmMap = new int[1][4]; int[] crops = new int[2]; - B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, workingRange, plantCrops, + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, lineWorkingRange, + columnWorkingRange, plantCrops, farmMap, crops); B129.harvestCrops(farmer); @@ -78,7 +92,8 @@ public class B129Test { 10, 5 }; - B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, null, null, farmMap, + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, null, null, null, + farmMap, crops); StringBuilder actualResult = B129.outputResult(farmer); @@ -89,4 +104,4 @@ public class B129Test { + "..\r\n"; assertEquals(expectedString, actualResult.toString()); } -} \ No newline at end of file +} -- GitLab From de76deb8c9f8a7b49abc36f5d5e9435e5696f9e4 Mon Sep 17 00:00:00 2001 From: rooki Date: Thu, 24 Jul 2025 16:01:33 +0900 Subject: [PATCH 4/5] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB129=E3=81=AE?= =?UTF-8?q?=E5=9B=9E=E7=AD=94=E3=80=81=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E5=AF=BE=E3=81=97=E3=81=A6=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B129.java | 82 +++++++++++++++++++++++++++++++-------------- rooki/B129Test.java | 74 ++++++++++++++++++++++++++++------------ 2 files changed, 109 insertions(+), 47 deletions(-) diff --git a/rooki/B129.java b/rooki/B129.java index 4ecdfcc..c0955f7 100644 --- a/rooki/B129.java +++ b/rooki/B129.java @@ -1,3 +1,5 @@ +import java.util.HashMap; +import java.util.Map; import java.util.Scanner; /** @@ -32,15 +34,27 @@ public class B129 { // 作業回数 for (int times = 0; times < farmer.workingTimes; times++) { // 何行から何行まで植えるか - for (int line = - farmer.lineWorkingRange[times][0]; line <= farmer.lineWorkingRange[times][1]; line++) { + for (int row = + farmer.rowWorkingRange + .get("rowStartRange" + + times + + "times"); row <= farmer.rowWorkingRange + .get("rowEndRange" + + times + + "times"); row++) { // 何列から何列まで植えるか for (int column = - farmer.columnWorkingRange[times][0]; column <= farmer.columnWorkingRange[times][1]; column++) { - - countCrops(farmer, line, column); + farmer.columnWorkingRange + .get("columnStartRange" + + times + + "times"); column <= farmer.columnWorkingRange + .get("columnEndRange" + + times + + "times"); column++) { + + countCrops(farmer, row, column); // 指定範囲に指定作物を植える - farmer.farmMap[line - 1][column - 1] = farmer.plantCrops[times]; + farmer.farmMap[row - 1][column - 1] = farmer.plantCrops[times]; } } } @@ -50,14 +64,14 @@ public class B129 { * 作物の数を数える. * * @param farmer 農場の情報を持つ - * @param line 行数 + * @param row 行数 * @param column 列数 */ - public static void countCrops(final FarmInfo farmer, int line, int column) { + public static void countCrops(final FarmInfo farmer, int row, int column) { // 作物を収穫し、数える for (int i = 1; i <= farmer.categoryNumber; i++) { // もし作物があれば収穫する - if (i == farmer.farmMap[line - 1][column - 1]) { + if (i == farmer.farmMap[row - 1][column - 1]) { // 配列のインデックスと対応している箇所の作物の個数を増やす farmer.crops[i - 1]++; } @@ -78,8 +92,8 @@ public class B129 { + "\r\n"); } - for (int[] lineRange : farmer.farmMap) { - for (int farmStatus : lineRange) { + for (int[] rowRange : farmer.farmMap) { + for (int farmStatus : rowRange) { if (farmStatus == 0) { result.append("."); } else { @@ -98,8 +112,11 @@ public class B129 { static class FarmInfo { final int workingTimes; final int categoryNumber; - final int[][] lineWorkingRange; - final int[][] columnWorkingRange; + // final int[][] rowWorkingRange; + Map rowWorkingRange = new HashMap<>(); + + // final int[][] columnWorkingRange; + Map columnWorkingRange = new HashMap<>(); final int[] plantCrops; final int[][] farmMap; final int[] crops; @@ -107,13 +124,13 @@ public class B129 { /** * テストコードで呼び出す際に利用するコンストラクタ. */ - public FarmInfo(int workingTimes, int categoryNumber, int[][] lineWorkingRange, - int[][] columnWorkingRange, int[] plantCrops, + public FarmInfo(int workingTimes, int categoryNumber, Map rowWorkingRange, + Map columnWorkingRange, int[] plantCrops, int[][] farmMap, int[] crops) { this.workingTimes = workingTimes; this.categoryNumber = categoryNumber; - this.lineWorkingRange = lineWorkingRange; + this.rowWorkingRange = rowWorkingRange; this.columnWorkingRange = columnWorkingRange; this.plantCrops = plantCrops; this.farmMap = farmMap; @@ -129,28 +146,43 @@ public class B129 { public static FarmInfo createFrom(final Scanner sc) { int workingTimes = sc.nextInt(); // 1年の作業回数 int categoryNumber = sc.nextInt(); // 作物の種類数 - int lineCount = sc.nextInt(); // 行数 + int rowCount = sc.nextInt(); // 行数 int columnCount = sc.nextInt(); // 列数 - int[][] farmMap = new int[lineCount][columnCount]; // 指定したサイズの農場 + int[][] farmMap = new int[rowCount][columnCount]; // 指定したサイズの農場 int[] crops = new int[categoryNumber]; // 収穫した作物の個数を保存する配列 int[] plantCrops = new int[workingTimes]; - int[][] lineWorkingRange = new int[workingTimes][2]; - int[][] columnWorkingRange = new int[workingTimes][2]; + // int[][] rowWorkingRange = new int[workingTimes][2]; + // int[][] columnWorkingRange = new int[workingTimes][2]; + + Map rowWorkingRange = new HashMap<>(); + Map columnWorkingRange = new HashMap<>(); for (int times = 0; times < workingTimes; times++) { - lineWorkingRange[times][0] = sc.nextInt(); // 作業範囲を保存 - lineWorkingRange[times][1] = sc.nextInt(); - columnWorkingRange[times][0] = sc.nextInt(); - columnWorkingRange[times][1] = sc.nextInt(); + rowWorkingRange + .put("rowStartRange" + + times + + "times", sc.nextInt()); // 作業範囲を保存 + rowWorkingRange + .put("rowEndRange" + + times + + "times", sc.nextInt()); + columnWorkingRange + .put("columnStartRange" + + times + + "times", sc.nextInt()); + columnWorkingRange + .put("columnEndRange" + + times + + "times", sc.nextInt()); plantCrops[times] = sc.nextInt(); // 植える作物の種類を保存 } sc.close(); // 読み込んだデータを使って、新しいインスタンスを生成して返す - return new FarmInfo(workingTimes, categoryNumber, lineWorkingRange, columnWorkingRange, + return new FarmInfo(workingTimes, categoryNumber, rowWorkingRange, columnWorkingRange, plantCrops, farmMap, crops); } } diff --git a/rooki/B129Test.java b/rooki/B129Test.java index 6b562ff..5fb5ce8 100644 --- a/rooki/B129Test.java +++ b/rooki/B129Test.java @@ -1,4 +1,6 @@ import static org.junit.Assert.*; +import java.util.HashMap; +import java.util.Map; import org.junit.Test; /** @@ -13,15 +15,25 @@ public class B129Test { public void 植えられていない箇所に1を植えた際に最終状態が一致しているか() throws Exception { int workingTimes = 1; int categoryNumber = 2; - int[][] lineWorkingRange = { - { - 1, 1 - } + Map rowWorkingRange = new HashMap() { + { + put("rowStartRange" + + 0 + + "times", 1); + put("rowEndRange" + + 0 + + "times", 1); + } }; - int[][] columnWorkingRange = { - { - 1, 1 - } + Map columnWorkingRange = new HashMap() { + { + put("columnStartRange" + + 0 + + "times", 1); + put("columnEndRange" + + 0 + + "times", 1); + } }; int[] plantCrops = { 1 @@ -29,7 +41,7 @@ public class B129Test { int[][] farmMap = new int[1][4]; int[] crops = new int[2]; - B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, lineWorkingRange, + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, rowWorkingRange, columnWorkingRange, plantCrops, farmMap, crops); @@ -46,19 +58,37 @@ public class B129Test { public void 作物1を1つ収穫した際に収穫数が一致しているか() throws Exception { int workingTimes = 2; int categoryNumber = 2; - int[][] lineWorkingRange = { - { - 1, 1 - }, { - 1, 1 - } + Map rowWorkingRange = new HashMap() { + { + put("rowStartRange" + + 0 + + "times", 1); + put("rowEndRange" + + 0 + + "times", 1); + put("rowStartRange" + + 1 + + "times", 1); + put("rowEndRange" + + 1 + + "times", 1); + } }; - int[][] columnWorkingRange = { - { - 1, 1 - }, { - 1, 1 - } + Map columnWorkingRange = new HashMap() { + { + put("columnStartRange" + + 0 + + "times", 1); + put("columnEndRange" + + 0 + + "times", 1); + put("columnStartRange" + + 1 + + "times", 1); + put("columnEndRange" + + 1 + + "times", 1); + } }; int[] plantCrops = { 1, 1 @@ -66,7 +96,7 @@ public class B129Test { int[][] farmMap = new int[1][4]; int[] crops = new int[2]; - B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, lineWorkingRange, + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, rowWorkingRange, columnWorkingRange, plantCrops, farmMap, crops); -- GitLab From 6a6cf9909b59829cf41c7f6e8c1e4f4e5ac9e4f9 Mon Sep 17 00:00:00 2001 From: rooki Date: Mon, 28 Jul 2025 10:08:55 +0900 Subject: [PATCH 5/5] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB129=E3=81=AE?= =?UTF-8?q?=E5=9B=9E=E7=AD=94=E3=80=81=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=AE=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E5=AF=BE=E3=81=97=E3=81=A6=E3=81=AE=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B129.java | 112 +++++++++++++++++++------------------------- rooki/B129Test.java | 86 +++++++--------------------------- 2 files changed, 64 insertions(+), 134 deletions(-) diff --git a/rooki/B129.java b/rooki/B129.java index c0955f7..fbb0d03 100644 --- a/rooki/B129.java +++ b/rooki/B129.java @@ -1,12 +1,12 @@ -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import java.util.Scanner; /** * 農場作業が終わった後の収穫個数と作物状況を出力. * * @author rooki - * @version 3.0 + * @version 4.0 */ public class B129 { @@ -32,29 +32,13 @@ public class B129 { */ public static void harvestCrops(final FarmInfo farmer) { // 作業回数 - for (int times = 0; times < farmer.workingTimes; times++) { - // 何行から何行まで植えるか - for (int row = - farmer.rowWorkingRange - .get("rowStartRange" - + times - + "times"); row <= farmer.rowWorkingRange - .get("rowEndRange" - + times - + "times"); row++) { - // 何列から何列まで植えるか - for (int column = - farmer.columnWorkingRange - .get("columnStartRange" - + times - + "times"); column <= farmer.columnWorkingRange - .get("columnEndRange" - + times - + "times"); column++) { + for (FarmInfo.Working work : farmer.works) { + for (int row = work.rowStartRange; row <= work.rowEndRange; row++) { + for (int column = work.columnStartRange; column <= work.columnEndRange; column++) { countCrops(farmer, row, column); // 指定範囲に指定作物を植える - farmer.farmMap[row - 1][column - 1] = farmer.plantCrops[times]; + farmer.farmMap[row - 1][column - 1] = work.plantCrop; } } } @@ -112,29 +96,45 @@ public class B129 { static class FarmInfo { final int workingTimes; final int categoryNumber; - // final int[][] rowWorkingRange; - Map rowWorkingRange = new HashMap<>(); - // final int[][] columnWorkingRange; - Map columnWorkingRange = new HashMap<>(); - final int[] plantCrops; + //final int plantCrop; final int[][] farmMap; final int[] crops; + final List works; + + /** + * 1回分の作業範囲の情報を持つクラス. + * + */ + static class Working { + int rowStartRange; + int rowEndRange; + int columnStartRange; + int columnEndRange; + int plantCrop; + + public Working(final int rowStartRange, final int rowEndRange, final int columnStartRange, + final int columnEndRange, final int plantCrop) { + this.rowStartRange = rowStartRange; + this.rowEndRange = rowEndRange; + this.columnStartRange = columnStartRange; + this.columnEndRange = columnEndRange; + this.plantCrop = plantCrop; + } + + } + /** * テストコードで呼び出す際に利用するコンストラクタ. */ - public FarmInfo(int workingTimes, int categoryNumber, Map rowWorkingRange, - Map columnWorkingRange, int[] plantCrops, - int[][] farmMap, - int[] crops) { + public FarmInfo(int workingTimes, int categoryNumber, int[][] farmMap, int[] crops, + List works) { this.workingTimes = workingTimes; this.categoryNumber = categoryNumber; - this.rowWorkingRange = rowWorkingRange; - this.columnWorkingRange = columnWorkingRange; - this.plantCrops = plantCrops; this.farmMap = farmMap; this.crops = crops; + this.works = works; } /** @@ -152,38 +152,22 @@ public class B129 { int[][] farmMap = new int[rowCount][columnCount]; // 指定したサイズの農場 int[] crops = new int[categoryNumber]; // 収穫した作物の個数を保存する配列 - int[] plantCrops = new int[workingTimes]; - - // int[][] rowWorkingRange = new int[workingTimes][2]; - // int[][] columnWorkingRange = new int[workingTimes][2]; - - Map rowWorkingRange = new HashMap<>(); - Map columnWorkingRange = new HashMap<>(); - - for (int times = 0; times < workingTimes; times++) { - rowWorkingRange - .put("rowStartRange" - + times - + "times", sc.nextInt()); // 作業範囲を保存 - rowWorkingRange - .put("rowEndRange" - + times - + "times", sc.nextInt()); - columnWorkingRange - .put("columnStartRange" - + times - + "times", sc.nextInt()); - columnWorkingRange - .put("columnEndRange" - + times - + "times", sc.nextInt()); - plantCrops[times] = sc.nextInt(); // 植える作物の種類を保存 + List works = new ArrayList<>(); + + for (int i = 0; i < workingTimes; i++) { + int rowStartRange = sc.nextInt(); + int rowEndRange = sc.nextInt(); + int columnStartRange = sc.nextInt(); + int columnEndRange = sc.nextInt(); + int cropType = sc.nextInt(); + works + .add(new Working(rowStartRange, rowEndRange, columnStartRange, columnEndRange, + cropType)); } sc.close(); - // 読み込んだデータを使って、新しいインスタンスを生成して返す - return new FarmInfo(workingTimes, categoryNumber, rowWorkingRange, columnWorkingRange, - plantCrops, farmMap, crops); + return new FarmInfo(workingTimes, categoryNumber, farmMap, crops, works); } + } } diff --git a/rooki/B129Test.java b/rooki/B129Test.java index 5fb5ce8..8d24bb0 100644 --- a/rooki/B129Test.java +++ b/rooki/B129Test.java @@ -1,13 +1,13 @@ import static org.junit.Assert.*; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import org.junit.Test; /** * B129のテストコード. * * @author rooki - * @version 3.0 + * @version 4.0 */ public class B129Test { @@ -15,35 +15,13 @@ public class B129Test { public void 植えられていない箇所に1を植えた際に最終状態が一致しているか() throws Exception { int workingTimes = 1; int categoryNumber = 2; - Map rowWorkingRange = new HashMap() { - { - put("rowStartRange" - + 0 - + "times", 1); - put("rowEndRange" - + 0 - + "times", 1); - } - }; - Map columnWorkingRange = new HashMap() { - { - put("columnStartRange" - + 0 - + "times", 1); - put("columnEndRange" - + 0 - + "times", 1); - } - }; - int[] plantCrops = { - 1 - }; int[][] farmMap = new int[1][4]; int[] crops = new int[2]; - B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, rowWorkingRange, - columnWorkingRange, plantCrops, - farmMap, crops); + List works = new ArrayList<>(); + + works.add(new B129.FarmInfo.Working(1, 1, 1, 1, 1)); + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, farmMap, crops, works); B129.harvestCrops(farmer); @@ -58,47 +36,15 @@ public class B129Test { public void 作物1を1つ収穫した際に収穫数が一致しているか() throws Exception { int workingTimes = 2; int categoryNumber = 2; - Map rowWorkingRange = new HashMap() { - { - put("rowStartRange" - + 0 - + "times", 1); - put("rowEndRange" - + 0 - + "times", 1); - put("rowStartRange" - + 1 - + "times", 1); - put("rowEndRange" - + 1 - + "times", 1); - } - }; - Map columnWorkingRange = new HashMap() { - { - put("columnStartRange" - + 0 - + "times", 1); - put("columnEndRange" - + 0 - + "times", 1); - put("columnStartRange" - + 1 - + "times", 1); - put("columnEndRange" - + 1 - + "times", 1); - } - }; - int[] plantCrops = { - 1, 1 - }; int[][] farmMap = new int[1][4]; int[] crops = new int[2]; - B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, rowWorkingRange, - columnWorkingRange, plantCrops, - farmMap, crops); + List works = new ArrayList<>(); + + works.add(new B129.FarmInfo.Working(1, 1, 1, 1, 1)); + works.add(new B129.FarmInfo.Working(1, 1, 1, 1, 1)); + + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, farmMap, crops, works); B129.harvestCrops(farmer); @@ -122,9 +68,9 @@ public class B129Test { 10, 5 }; - B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, null, null, null, - farmMap, - crops); + List works = new ArrayList<>(); + + B129.FarmInfo farmer = new B129.FarmInfo(workingTimes, categoryNumber, farmMap, crops, works); StringBuilder actualResult = B129.outputResult(farmer); -- GitLab