diff --git a/knoda/src/C018_Recipe.java b/knoda/src/C018_Recipe.java new file mode 100644 index 0000000000000000000000000000000000000000..1370d02c165ad5b375fa4124c909af1304d41380 --- /dev/null +++ b/knoda/src/C018_Recipe.java @@ -0,0 +1,89 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; + +public class C018_Recipe { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + // foodstuffMap : レシピに書かれている食材とその個数が格納されるマップ + final Map foodstuffMap = new LinkedHashMap(); + + // レシピに書かれている食材の個数を入力 + int foodstuffOfRecipeNum = scan.nextInt(); + + // レシピに書かれているそれぞれの食材とその個数を入力 + for (int foodstuff = 0; foodstuff < foodstuffOfRecipeNum; foodstuff++) { + String foodstuffName = scan.next(); + int foodstuffNum = scan.nextInt(); + + if (foodstuffMap.containsKey(foodstuffName)) { + // 食材が重複している場合はその個数を更新する + foodstuffMap.replace(foodstuffName, foodstuffMap.get(foodstuffName) + foodstuffNum); + } else { + // 食材が重複していない場合は新たに食材とその個数をfoodstuffMapに格納する + foodstuffMap.put(foodstuffName, foodstuffNum); + } + } + + // haveFoodstuffMap : 自身が所持している食材とその個数が格納されるマップ + final Map haveFoodstuffMap = new LinkedHashMap(); + + // 自身が所持している食材の個数を入力 + int haveFoodstuffNum = scan.nextInt(); + + // 自身が所持しているそれぞれの食材とその個数を入力 + for (int foodstuff = 0; foodstuff < haveFoodstuffNum; foodstuff++) { + String foodstuffName = scan.next(); + int foodstuffNum = scan.nextInt(); + + if (haveFoodstuffMap.containsKey(foodstuffName)) { + // 食材が重複している場合はその個数を更新する + haveFoodstuffMap.replace(foodstuffName, haveFoodstuffMap.get(foodstuffName) + foodstuffNum); + } else { + // 食材が重複していない場合は新たに食材とその個数をfoodstuffMapに格納する + haveFoodstuffMap.put(foodstuffName, foodstuffNum); + } + } + + // 何人前のレシピを作ることができるかを取得する + int people = howManyService(foodstuffMap, haveFoodstuffMap); + System.out.println(people); + + scan.close(); + } + + // 何人前のレシピを作ることができるかを取得するメソッド + static int howManyService(final Map foodstuffMap, final Map haveFoodstuffMap) { + final Set foodstuffSet = foodstuffMap.keySet(); + + // foodstuffNumList : レシピに書かれているそれぞれの食材の個数が格納されるリスト + final List foodstuffNumList = new ArrayList(); + + // haveFoodstuffNumList : 自身が所持している食材がレシピに書かれている場合、その個数が格納されるリスト + final List haveFoodstuffNumList = new ArrayList(); + + if (haveFoodstuffMap.keySet().containsAll(foodstuffMap.keySet())) { + + int people = 10000; + for (String foodstuff : foodstuffSet) { + int foodstuffPeople = haveFoodstuffMap.get(foodstuff) / foodstuffMap.get(foodstuff); + if (people > foodstuffPeople) { + people = foodstuffPeople; + } + } + + return people; + + } else { + return 0; + } + } + +} diff --git a/knoda/src/C065_AnswerNumber.class b/knoda/src/C065_AnswerNumber.class new file mode 100644 index 0000000000000000000000000000000000000000..123ca7f334f30bc4dfdd83fffbc0a621af717bf7 Binary files /dev/null and b/knoda/src/C065_AnswerNumber.class differ diff --git a/knoda/src/C065_AnswerNumber.java b/knoda/src/C065_AnswerNumber.java new file mode 100644 index 0000000000000000000000000000000000000000..ac3d30051a40833d793ce35a633b74f0e2a9a9a4 --- /dev/null +++ b/knoda/src/C065_AnswerNumber.java @@ -0,0 +1,106 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class C065_AnswerNumber { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + // ヒントの数を入力 + final int hintNum = scan.nextInt(); + + /* symbolList : 記号(<、>、/)が格納されているリスト + * numList : 整数が格納されているリスト + */ + final List symbolList = new ArrayList(); + final List numList = new ArrayList(); + + // 記号と整数を入力 + for (int hint = 0; hint < hintNum; hint++) { + symbolList.add(scan.next()); + numList.add(scan.nextInt()); + } + + final int answer = getAnswer(symbolList, numList); + System.out.println(answer); + scan.close(); + } + + // ヒントの条件を満たす整数を取得するメソッド + static int getAnswer(List symbolList, List numList) { + final List answerCandidateNumList + = hintCalculation(symbolList, numList); + + int answer = 0; + + // 整数がいくつ条件を満たしているか数える + for (Integer num : answerCandidateNumList) { + long numCount = answerCandidateNumList + .stream() + .filter(n -> n == num) + .count(); + + // 整数がすべての条件を満たしているとき + if (numCount == symbolList.size()) { + answer = num; + break; + } + } + return answer; + } + + // ヒントの記号によって取得方法を変えるメソッド + static List hintCalculation(List symbolList, List numList) { + + // 1つでもヒントの条件を満たす整数を格納するリスト + List answerCandidateNumList = new ArrayList(); + + for (int hint = 0; hint < symbolList.size(); hint++) { + for (int num = 1; num <= 100; num++) { + + // ヒントの記号が「>」「<」「/」のみのとき + if (symbolList.contains(">") || symbolList.contains("<") + || symbolList.contains("/")) { + answerCandidateNumList = calculation( + symbolList.get(hint), numList.get(hint), + num, answerCandidateNumList); + } else { + removeList(answerCandidateNumList); + break; + } + } + } + return answerCandidateNumList; + } + + // ヒントの条件を満たすための計算処理メソッド + static List calculation(String symbol, int hintNum, + int num, List answerCandidateNumList) { + + // ヒントの条件を満たしているかを判定 + if (">".equals(symbol)) { + if (num > hintNum) { + answerCandidateNumList.add(num); + } + } else if ("<".equals(symbol)) { + if (num < hintNum) { + answerCandidateNumList.add(num); + } + } else if ("/".equals(symbol)) { + if (num % hintNum == 0) { + answerCandidateNumList.add(num); + } + } + return answerCandidateNumList; + } + + // リストの中身を削除するメソッド + static void removeList(List list) { + while (list.size() > 0) { + list.remove(0); + } + } +} diff --git a/knoda/src/C069_Festival.java b/knoda/src/C069_Festival.java new file mode 100644 index 0000000000000000000000000000000000000000..136d7c716900684b66faba7fb854d798e92d10fa --- /dev/null +++ b/knoda/src/C069_Festival.java @@ -0,0 +1,77 @@ +package hellojunit; + +import java.util.Scanner; + +public class C069_Festival { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + final int year = scan.nextInt(); + final int month = scan.nextInt(); + final int day = scan.nextInt(); + final int holdMonth = scan.nextInt(); + final int holdDay = scan.nextInt(); + + Festival festival = new Festival(year, month, day, holdMonth, holdDay); + System.out.println(festival.dayUntilFestival()); + + scan.close(); + } +} + +class Festival { + private int year; + private int month; + private int day; + private final int holdMonth; + private final int holdDay; + + public Festival(final int year, final int month, final int day, final int holdMonth, final int holdDay) { + this.year = year; + this.month = month; + this.day = day; + this.holdMonth = holdMonth; + this.holdDay = holdDay; + } + + // ある月の日数を取得するメソッド + private int daysOfMonth(int month) { + if (month % 2 == 0) { + return 15; + } + return 13; + } + + // 祭りが開催される年であるかを判定するメソッド + private boolean isHoldFestivalYear(int year) { + return year % 4 == 1; + } + + // 年と月をカウントアップするメソッド + private void countUpYearAndMonth() { + month++; + if (month > 13) { + month = 1; + year++; + } + } + + // 祭り開催までの残り日数を取得するメソッド + public int dayUntilFestival() { + int holdYear = year + 1; + int nokoriDay = 0; + while (!isHoldFestivalYear(holdYear)) { + holdYear++; + } + + nokoriDay += daysOfMonth(month) - day; + countUpYearAndMonth(); + + while ((year != holdYear) || (month != holdMonth)) { + nokoriDay += daysOfMonth(month); + countUpYearAndMonth(); + } + nokoriDay += holdDay; + return nokoriDay; + } +} diff --git a/knoda/test/C018_RecipeTest.java b/knoda/test/C018_RecipeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b4fc8aee9801b356175e91d9cb26711835d914cf --- /dev/null +++ b/knoda/test/C018_RecipeTest.java @@ -0,0 +1,72 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Test; + +public class C018_RecipeTest { + + @Test + public void 出力が2になるテスト() { + Map foodstuffMap = new LinkedHashMap(); + Map haveFoodstuffMap = new LinkedHashMap(); + + foodstuffMap.put("supaisu", 5); + foodstuffMap.put("imo", 2); + foodstuffMap.put("niku", 2); + foodstuffMap.put("mizu", 3); + + haveFoodstuffMap.put("mizu", 7); + haveFoodstuffMap.put("imo", 4); + haveFoodstuffMap.put("ninjin", 10); + haveFoodstuffMap.put("unagi", 6); + haveFoodstuffMap.put("supaisu", 20); + haveFoodstuffMap.put("niku", 5); + + int actual = C018_Recipe.howManyService(foodstuffMap, haveFoodstuffMap); + int expected = 2; + assertThat(actual, is(expected)); + } + + @Test + public void 出力が3になるテスト() { + Map foodstuffMap = new LinkedHashMap(); + Map haveFoodstuffMap = new LinkedHashMap(); + + foodstuffMap.put("supaisu", 5); + foodstuffMap.put("imo", 2); + foodstuffMap.put("niku", 2); + foodstuffMap.put("mizu", 3); + + haveFoodstuffMap.put("mizu", 10); + haveFoodstuffMap.put("imo", 4); + haveFoodstuffMap.put("supaisu", 20); + haveFoodstuffMap.replace("imo", haveFoodstuffMap.get("imo") + 2); + haveFoodstuffMap.put("niku", 1); + haveFoodstuffMap.replace("niku", haveFoodstuffMap.get("niku") + 5); + + int actual = C018_Recipe.howManyService(foodstuffMap, haveFoodstuffMap); + int expected = 3; + assertThat(actual, is(expected)); + } + + @Test + public void 出力が0になるテスト() { + Map foodstuffMap = new LinkedHashMap(); + Map haveFoodstuffMap = new LinkedHashMap(); + + foodstuffMap.put("gohan", 1); + foodstuffMap.put("okazu", 1); + + haveFoodstuffMap.put("mizu", 10000); + + int actual = C018_Recipe.howManyService(foodstuffMap, haveFoodstuffMap); + int expected = 0; + assertThat(actual, is(expected)); + } + +} diff --git a/knoda/test/C065_AnswerNumberTest.java b/knoda/test/C065_AnswerNumberTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1cf005d26acb702b7ff10e034f66a1f498ba4d56 --- /dev/null +++ b/knoda/test/C065_AnswerNumberTest.java @@ -0,0 +1,106 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; + +@RunWith(Enclosed.class) +public class C065_AnswerNumberTest { + + public static class + ヒントの記号が大なり小なりスラッシュのみかつ正の整数が100以下のとき { + + List symbolList; + List numList; + + @Test + public void 出力が35になるテスト() { + symbolList = new ArrayList(Arrays.asList(">", "<", "/")); + numList = new ArrayList(Arrays.asList(30, 40, 5)); + int actual = C065_AnswerNumber.getAnswer(symbolList, numList); + int expected = 35; + assertThat(actual, is(expected)); + } + + @Test + public void 出力が84になるテスト() { + symbolList = new ArrayList(Arrays.asList("/", "<", "/", ">")); + numList = new ArrayList(Arrays.asList(4, 90, 6, 77)); + int actual = C065_AnswerNumber.getAnswer(symbolList, numList); + int expected = 84; + assertThat(actual, is(expected)); + } + } + + public static class + ヒントの記号が大なり小なりスラッシュ以外を含むとき { + + List symbolList; + List numList; + + @Test + public void 出力が0になるテスト1() { + symbolList = new ArrayList(Arrays.asList(">", "<", "/", "+")); + numList = new ArrayList(Arrays.asList(30, 40, 5, 4)); + int actual = C065_AnswerNumber.getAnswer(symbolList, numList); + int expected = 0; + assertThat(actual, is(expected)); + } + + @Test + public void 出力が0になるテスト2() { + symbolList = new ArrayList(Arrays.asList("/", "<", "*", "/", ">")); + numList = new ArrayList(Arrays.asList(4, 90, 12, 6, 77)); + int actual = C065_AnswerNumber.getAnswer(symbolList, numList); + int expected = 0; + assertThat(actual, is(expected)); + } + } + + public static class + ヒントが100以上の整数を含むとき { + + List symbolList; + List numList; + + @Test + public void 出力が0になるテスト() { + symbolList = new ArrayList(Arrays.asList("/", "<", "/", ">")); + numList = new ArrayList(Arrays.asList(20, 200, 5, 100)); + int actual = C065_AnswerNumber.getAnswer(symbolList, numList); + int expected = 0; + assertThat(actual, is(expected)); + } + + } + + public static class 条件を満たす整数が一意でない { + List symbolList; + List numList; + + @Test + public void 出力が32と36の2つあるのに32のみが出力されるテスト() { + symbolList = new ArrayList(Arrays.asList(">", "<", "/")); + numList = new ArrayList(Arrays.asList(30, 40, 4)); + int actual = C065_AnswerNumber.getAnswer(symbolList, numList); + int expected = 32; + assertThat(actual, is(expected)); + } + + @Test + public void 出力が15の倍数すべてがあるのに15のみが出力されるテスト() { + symbolList = new ArrayList(Arrays.asList("/", "/")); + numList = new ArrayList(Arrays.asList(3, 5)); + int actual = C065_AnswerNumber.getAnswer(symbolList, numList); + int expected = 15; + assertThat(actual, is(expected)); + } + } +} diff --git a/knoda/test/C069_FestivalTest.java b/knoda/test/C069_FestivalTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9bd2d667d3d381be436a72b7ea38f8eb3659a8d3 --- /dev/null +++ b/knoda/test/C069_FestivalTest.java @@ -0,0 +1,26 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class C069_FestivalTest { + + @Test + public void 日数を取得するテスト1() { + Festival sut = new Festival(2000, 12, 10, 1, 10); + int expected = 28; + int actual = sut.dayUntilFestival(); + assertThat(actual, is(expected)); + } + + @Test + public void 日数を取得するテスト2() { + Festival sut = new Festival(1994, 4, 8, 7, 13); + int expected = 591; + int actual = sut.dayUntilFestival(); + assertThat(actual, is(expected)); + } + +}