diff --git a/ykoiso/src/A059_Goldfish.java b/ykoiso/src/A059_Goldfish.java new file mode 100644 index 0000000000000000000000000000000000000000..833efc2bdb162b3458ae1c383a34ff6bfb0b035f --- /dev/null +++ b/ykoiso/src/A059_Goldfish.java @@ -0,0 +1,64 @@ +import java.util.Scanner; + +public class A059_Goldfish { + public static void main(String[] args) { + final Scanner scan = new Scanner(System.in); + // 金魚の種類の数 + final int goldfishNum = scan.nextInt(); + // ポイの強度 + final int poisStrength = scan.nextInt(); + // 金魚の重さ + final int[] goldfishsWeight = new int[goldfishNum]; + // 金魚の美しさ + final int[] goldfishsBeauty = new int[goldfishNum]; + for (int i = 0; i < goldfishNum; i++) { + goldfishsWeight[i] = scan.nextInt(); + goldfishsBeauty[i] = scan.nextInt(); + } + A059_Goldfish gf = new A059_Goldfish(); + // Goldfishクラスのコンストラクタの呼び出し + Goldfish goldfish = gf.new Goldfish(goldfishsWeight, goldfishsBeauty, goldfishNum); + // ポイを壊さずに得られる最大の美しさを出力 + System.out.println(goldfish.calcMaxBeautyPoint(poisStrength)); + scan.close(); + } + + class Goldfish { + private int[] weight; + private int[] beauty; + private int num; + + // コンストラクタ + Goldfish(final int[] weight, final int[] beauty, final int num) { + this.weight = weight.clone(); + this.beauty = beauty.clone(); + this.num = num; + } + + // ポイを壊さずに得られる最大の美しさを計算 + public int calcMaxBeautyPoint(final int poisStrength) { + // 動的計画法(Dynamic Programming) + // 動的計画法の漸化式の答え + int[][] dynamicProgrammingBeautyPoint = new int[num + 1][poisStrength + 1]; + // 最大値の記憶 + int maxBeautyPoint = 0; + // ある大きさに対して価値が大きくなる方を記憶する + // 繰り返して最大値を計算 + for (int i = 0; i < num; i++) { + for (int j = 0; j <= poisStrength; j++) { + dynamicProgrammingBeautyPoint[i + 1][j] = + Math.max(dynamicProgrammingBeautyPoint[i + 1][j], + dynamicProgrammingBeautyPoint[i][j]); + if (j + weight[i] < poisStrength) { + dynamicProgrammingBeautyPoint[i + 1][j + weight[i]] = + Math.max(dynamicProgrammingBeautyPoint[i + 1][j + weight[i]], + dynamicProgrammingBeautyPoint[i][j] + beauty[i]); + maxBeautyPoint = Math.max(maxBeautyPoint, + dynamicProgrammingBeautyPoint[i + 1][j + weight[i]]); + } + } + } + return maxBeautyPoint; + } + } +} diff --git a/ykoiso/test/A059_GoldfishTest.java b/ykoiso/test/A059_GoldfishTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ef8e1c5641de21518be4891e02078320c743c81b --- /dev/null +++ b/ykoiso/test/A059_GoldfishTest.java @@ -0,0 +1,55 @@ +import static org.junit.Assert.assertThat; +import org.junit.Test; +import static org.hamcrest.CoreMatchers.*; + +public class A059_GoldfishTest { + final int goldfishNum=3; + final int poisStrength =15; + A059_Goldfish gf = new A059_Goldfish(); + + @Test + public void 金魚1匹で美しさが最大になる場合() { + final int[] goldfishsWeight={14,15,13}; + final int[] goldfishsBeauty={41,42,40}; + A059_Goldfish.Goldfish goldfish = gf.new Goldfish(goldfishsWeight, goldfishsBeauty, goldfishNum); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); + final int expected = 41; + assertThat(actual, is(expected)); + } + @Test + public void 金魚2匹で美しさが最大になる場合() { + final int[] goldfishsWeight={6,8,13}; + final int[] goldfishsBeauty={41,42,40}; + A059_Goldfish.Goldfish goldfish = gf.new Goldfish(goldfishsWeight, goldfishsBeauty, goldfishNum); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); + final int expected = 83; + assertThat(actual, is(expected)); + } + @Test + public void 金魚2匹より1匹のほうが美しさが最大になる場合() { + final int[] goldfishsWeight={6,8,14}; + final int[] goldfishsBeauty={21,22,44}; + A059_Goldfish.Goldfish goldfish = gf.new Goldfish(goldfishsWeight, goldfishsBeauty, goldfishNum); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); + final int expected = 44; + assertThat(actual, is(expected)); + } + @Test + public void 該当する金魚がいないとき0を返す() { + final int[] goldfishsWeight={15,17,19}; + final int[] goldfishsBeauty={21,22,44}; + A059_Goldfish.Goldfish goldfish = gf.new Goldfish(goldfishsWeight, goldfishsBeauty, goldfishNum); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); + final int expected = 0; + assertThat(actual, is(expected)); + } + @Test + public void 金魚の重さがポイの強度と同じだとカウントされない() { + final int[] goldfishsWeight={15,15,15}; + final int[] goldfishsBeauty={21,22,44}; + A059_Goldfish.Goldfish goldfish = gf.new Goldfish(goldfishsWeight, goldfishsBeauty, goldfishNum); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); + final int expected = 0; + assertThat(actual, is(expected)); + } +} \ No newline at end of file