diff --git a/kshiraishi/src/B157.java b/kshiraishi/src/B157.java new file mode 100644 index 0000000000000000000000000000000000000000..133eda2e0ba114cc5deef4217eda989a9b3d6cc7 --- /dev/null +++ b/kshiraishi/src/B157.java @@ -0,0 +1,70 @@ +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のインデックスを保存し、その数を返す + * + * @param shops スーパーのリスト + * @return 各野菜の最安値を更新するために必要なスーパーの数 + * */ + public static int findCheapestShop(List shops) { + if (shops.isEmpty()) { + return 0; // ショップが一つもなければ、必要な店舗数は0だよ + } + Set uniqueShopsNeeded = new HashSet<>(); + 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++) { //店舗数ループ + final Supermarket currentShop = shops.get(shopIndex); + final 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 0000000000000000000000000000000000000000..d94917e9d0080fbf19833d81d8c828aa1911ff73 --- /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 getlength() { + return this.prices.length; + } + + +} diff --git a/kshiraishi/test/B157Test.java b/kshiraishi/test/B157Test.java new file mode 100644 index 0000000000000000000000000000000000000000..0149eaf9c2f7e2c428683dca7839d488e3b99432 --- /dev/null +++ b/kshiraishi/test/B157Test.java @@ -0,0 +1,141 @@ +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) { + // 例外が発生したらテストを失敗させる + fail("予期しない例外:" + e.getMessage()); + } + } + + + @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店舗が必要 + }, + // <追加>テストケース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のみ + }, + }; + } + + @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 0000000000000000000000000000000000000000..2d2f572b6ecdc8f29d31c916722320c3a4ffcafd --- /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.getlength(),is(4)); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testGetPrice_IndexOutOfBounds() { + supermarket.getPrice(5); + } + } + + + } +