From 813f77d6422ca17df68626aa34dcd835495d02d1 Mon Sep 17 00:00:00 2001 From: kshiraishi Date: Fri, 25 Jul 2025 13:43:12 +0900 Subject: [PATCH 1/2] =?UTF-8?q?B157=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kshiraishi/src/B157.java | 66 +++++++++++++ kshiraishi/src/Supermarket.java | 18 ++++ kshiraishi/test/B157Test.java | 133 +++++++++++++++++++++++++++ kshiraishi/test/SupermarketTest.java | 46 +++++++++ 4 files changed, 263 insertions(+) create mode 100644 kshiraishi/src/B157.java create mode 100644 kshiraishi/src/Supermarket.java create mode 100644 kshiraishi/test/B157Test.java create mode 100644 kshiraishi/test/SupermarketTest.java diff --git a/kshiraishi/src/B157.java b/kshiraishi/src/B157.java new file mode 100644 index 0000000..5619f0b --- /dev/null +++ b/kshiraishi/src/B157.java @@ -0,0 +1,66 @@ +package B157; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Scanner; +import java.util.Set; + +/** それぞれの野菜を買うのに必要な店舗数を返す処理 */ +public class B157 { + + /**メイン 処理部分*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + final int supermarketNumber = sc.nextInt(); + final int vegetableType = sc.nextInt(); + + // 各スーパーのSupermarketオブジェクトを格納するリスト + List shops = new ArrayList<>(); + + for (int i = 0; i < supermarketNumber; i++) { + int[] prices = new int[vegetableType]; + for (int j = 0; j < vegetableType; j++) { + prices[j] = sc.nextInt(); + } + shops.add(new Supermarket(prices)); + } + sc.close(); + // ここまで初期化 + + // 各野菜の最安値をそれぞれのスーパーから探す + System.out.println(findCheapestShop(shops)); + + } + + /** + * 最安値を更新し、更新できたListのインデックスを保存し、その数を返す + * */ + public static int findCheapestShop(List shops) { + if (shops.isEmpty()) { + return 0; // ショップが一つもなければ、必要な店舗数は0だよ + } + Set uniqueShopsNeeded = new HashSet<>(); + for (int vegetableIndex = 0; vegetableIndex < shops.get(0).length(); vegetableIndex++) { //野菜の種類文ループ + int minPrice = Integer.MAX_VALUE; + int bestShopIndex = -1; + + for (int shopIndex = 0; shopIndex < shops.size(); shopIndex++) { //店舗数ループ + Supermarket currentShop = shops.get(shopIndex); + int currentPrice = currentShop.getPrice(vegetableIndex); + + //最安値とその店のインデックスを更新 + if (currentPrice < minPrice) { + minPrice = currentPrice; + bestShopIndex = shopIndex; + } + } + if (bestShopIndex != -1) { + uniqueShopsNeeded.add(bestShopIndex); //最安値を更新していればその店のインデックスを保存 + } + } + + return uniqueShopsNeeded.size(); //最安値を更新した店舗数を返す + } + +} diff --git a/kshiraishi/src/Supermarket.java b/kshiraishi/src/Supermarket.java new file mode 100644 index 0000000..4a831d1 --- /dev/null +++ b/kshiraishi/src/Supermarket.java @@ -0,0 +1,18 @@ +package B157; + +public class Supermarket { + private int[] prices; + + public Supermarket(int[] prices) { + this.prices = prices; + } + + public int getPrice(int vegetableIndex) { + return prices[vegetableIndex]; + } + public int length() { + return this.prices.length; + } + + +} diff --git a/kshiraishi/test/B157Test.java b/kshiraishi/test/B157Test.java new file mode 100644 index 0000000..1eb5ce9 --- /dev/null +++ b/kshiraishi/test/B157Test.java @@ -0,0 +1,133 @@ +package B157; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +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; + +@RunWith(Theories.class) +public class B157Test { + private final InputStream originalSystemIn = System.in; + + @Test + public void testMainMethod_入力例1() { + String input = "3 3\n" + // スーパー3つ、野菜3種類 + "100 200 300\n" + // スーパー1の価格 + "150 180 250\n" + // スーパー2の価格 + "90 220 280\n"; // スーパー3の価格 + + try (ByteArrayInputStream testInputStream = new ByteArrayInputStream(input.getBytes())) { + System.setIn(testInputStream); + + java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream(); + java.io.PrintStream originalOut = System.out; + System.setOut(new java.io.PrintStream(outContent)); + try { + // B157 の main メソッドを実行するんだ! + B157.main(new String[]{}); // mainメソッドはString[] argsを受け取るけど、今回は空でOK + + // mainメソッドが出力した内容を取得するよ + String actualOutput = outContent.toString().trim(); // 前後の空白をトリムするんだ + + // 期待される出力結果を計算するよ + // 上の入力データで findCheapestShop を実行した場合、結果は2になるはずだよね! + String expectedOutput = "2"; + + // 出力結果が期待通りかを確認するんだ! + assertEquals(expectedOutput, actualOutput); + + } finally { + System.setIn(originalSystemIn); + System.setOut(originalOut); // System.out も元に戻す + } + } catch (Exception e) { + // 例外が発生したらテストを失敗させるよ + e.printStackTrace(); + assertEquals("例外が発生したよ: " + e.getMessage(), true, false); + } + } + + + @DataPoints + public static Object[][] testCases(){ + return new Object[][] { + + // テストケース1: 基本的なケース (複数のスーパーから最安値を見つける) + { + Arrays.asList( + new Supermarket(new int[] {100,200,300}),//スーパー1 + new Supermarket(new int[] {150,180,250}),//スーパー2 + new Supermarket(new int[] {90,220,280}) //スーパー3 + ), + 2 //スーパー2とスーパー3 + }, + // テストケース2:すべての野菜が同じスーパーで最安値の場合 + { + Arrays.asList( + new Supermarket(new int[] {50,80,120}),//スーパー1 + new Supermarket(new int[] {60,90,130}) //スーパー2 + ), + 1 //スーパー1のみ + }, + // テストケース3: 最安値が同額で複数のスーパーにある場合 (最初のものが選ばれる) + { + Arrays.asList( + new Supermarket(new int[]{100}), // スーパー1 + new Supermarket(new int[]{100}) // スーパー2 (同額) + ), + 1 // スーパー1のみ(HashSetの性質上、最初に発見されたインデックスが入る) + }, + // テストケース4: スーパーが1つしかない場合 + { + Arrays.asList( + new Supermarket(new int[]{100, 200, 300}) + ), + 1 // その1つだけ + }, + // テストケース5: 野菜の種類が1つしかない場合 + { + Arrays.asList( + new Supermarket(new int[]{500}), + new Supermarket(new int[]{400}), // 最安値 + new Supermarket(new int[]{600}) + ), + 1 // 最安値のスーパー1つ + }, + // テストケース6: スーパーも野菜も存在しない場合 (空のリスト) + { + new ArrayList(), // 空のリスト + 0 // 0店舗 + }, + // テストケース7: 各野菜の最安値が異なるスーパーにある場合 + { + Arrays.asList( + new Supermarket(new int[]{10, 100, 100}), // スーパー0 (野菜A最安値) + new Supermarket(new int[]{100, 10, 100}), // スーパー1 (野菜B最安値) + new Supermarket(new int[]{100, 100, 10}) // スーパー2 (野菜C最安値) + ), + 3 // スーパー0, 1, 2の3店舗が必要 + } + }; + } + + @Theory + public void findCheapestShopのテスト(Object[] testCase) throws Exception { + List shops = (List) testCase[0]; + int expected = (int) testCase[1]; + + int actual = B157.findCheapestShop(shops); + + assertThat(actual, is(expected)); + + } + + +} diff --git a/kshiraishi/test/SupermarketTest.java b/kshiraishi/test/SupermarketTest.java new file mode 100644 index 0000000..6331927 --- /dev/null +++ b/kshiraishi/test/SupermarketTest.java @@ -0,0 +1,46 @@ +package B157; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.experimental.theories.Theories; +import org.junit.runner.RunWith; + +@RunWith(Enclosed.class) +public class SupermarketTest { + + + @RunWith(Theories.class) + public static class test{ + private Supermarket supermarket; + + @Before + public void setUp() { + int[] testPrices = {100, 150, 200, 80}; + supermarket = new Supermarket(testPrices); + } + + @Test + public void testGetPrice() { + assertThat(supermarket.getPrice(0),is(100)); + assertThat(supermarket.getPrice(1),is(150)); + assertThat(supermarket.getPrice(2),is(200)); + assertThat(supermarket.getPrice(3),is(80)); + } + + @Test + public void testLength() { + assertThat(supermarket.length(),is(4)); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testGetPrice_IndexOutOfBounds() { + supermarket.getPrice(5); + } + } + + + } + -- GitLab From ce26f363859a02946490fc73df52fde1212d22ae Mon Sep 17 00:00:00 2001 From: kshiraishi Date: Fri, 25 Jul 2025 16:52:00 +0900 Subject: [PATCH 2/2] =?UTF-8?q?B157=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kshiraishi/src/B157.java | 10 +++++++--- kshiraishi/src/Supermarket.java | 2 +- kshiraishi/test/B157Test.java | 16 ++++++++++++---- kshiraishi/test/SupermarketTest.java | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/kshiraishi/src/B157.java b/kshiraishi/src/B157.java index 5619f0b..133eda2 100644 --- a/kshiraishi/src/B157.java +++ b/kshiraishi/src/B157.java @@ -35,19 +35,23 @@ public class B157 { /** * 最安値を更新し、更新できたListのインデックスを保存し、その数を返す + * + * @param shops スーパーのリスト + * @return 各野菜の最安値を更新するために必要なスーパーの数 * */ public static int findCheapestShop(List shops) { if (shops.isEmpty()) { return 0; // ショップが一つもなければ、必要な店舗数は0だよ } Set uniqueShopsNeeded = new HashSet<>(); - for (int vegetableIndex = 0; vegetableIndex < shops.get(0).length(); vegetableIndex++) { //野菜の種類文ループ + final int vegetableCount = shops.get(0).getlength(); + for (int vegetableIndex = 0; vegetableIndex < vegetableCount; vegetableIndex++) { //野菜の種類文ループ int minPrice = Integer.MAX_VALUE; int bestShopIndex = -1; for (int shopIndex = 0; shopIndex < shops.size(); shopIndex++) { //店舗数ループ - Supermarket currentShop = shops.get(shopIndex); - int currentPrice = currentShop.getPrice(vegetableIndex); + final Supermarket currentShop = shops.get(shopIndex); + final int currentPrice = currentShop.getPrice(vegetableIndex); //最安値とその店のインデックスを更新 if (currentPrice < minPrice) { diff --git a/kshiraishi/src/Supermarket.java b/kshiraishi/src/Supermarket.java index 4a831d1..d94917e 100644 --- a/kshiraishi/src/Supermarket.java +++ b/kshiraishi/src/Supermarket.java @@ -10,7 +10,7 @@ public class Supermarket { public int getPrice(int vegetableIndex) { return prices[vegetableIndex]; } - public int length() { + public int getlength() { return this.prices.length; } diff --git a/kshiraishi/test/B157Test.java b/kshiraishi/test/B157Test.java index 1eb5ce9..0149eaf 100644 --- a/kshiraishi/test/B157Test.java +++ b/kshiraishi/test/B157Test.java @@ -49,9 +49,8 @@ public class B157Test { System.setOut(originalOut); // System.out も元に戻す } } catch (Exception e) { - // 例外が発生したらテストを失敗させるよ - e.printStackTrace(); - assertEquals("例外が発生したよ: " + e.getMessage(), true, false); + // 例外が発生したらテストを失敗させる + fail("予期しない例外:" + e.getMessage()); } } @@ -114,7 +113,16 @@ public class B157Test { new Supermarket(new int[]{100, 100, 10}) // スーパー2 (野菜C最安値) ), 3 // スーパー0, 1, 2の3店舗が必要 - } + }, + // <追加>テストケース8: すべてのスーパーで各野菜の値段が同一の場合 + { + Arrays.asList( + new Supermarket(new int[]{100, 200, 300}), // スーパー0 (野菜A最安値) + new Supermarket(new int[]{100, 200, 300}), // スーパー1 (野菜B最安値) + new Supermarket(new int[]{100, 200, 300}) // スーパー2 (野菜C最安値) + ), + 1 // スーパー1のみ + }, }; } diff --git a/kshiraishi/test/SupermarketTest.java b/kshiraishi/test/SupermarketTest.java index 6331927..2d2f572 100644 --- a/kshiraishi/test/SupermarketTest.java +++ b/kshiraishi/test/SupermarketTest.java @@ -32,7 +32,7 @@ public class SupermarketTest { @Test public void testLength() { - assertThat(supermarket.length(),is(4)); + assertThat(supermarket.getlength(),is(4)); } @Test(expected = ArrayIndexOutOfBoundsException.class) -- GitLab