From 4cfe92640f50c897c7e45a38dc67aed468a97f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Mon, 15 Aug 2022 12:35:37 +0900 Subject: [PATCH 1/2] =?UTF-8?q?A059=20=E9=87=91=E9=AD=9A=E3=81=AE=E7=BE=8E?= =?UTF-8?q?=E3=81=97=E3=81=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/A059_Goldfish.java | 62 ++++++++++++++++++++++++++++++ ykoiso/test/A059_GoldfishTest.java | 55 ++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 ykoiso/src/A059_Goldfish.java create mode 100644 ykoiso/test/A059_GoldfishTest.java diff --git a/ykoiso/src/A059_Goldfish.java b/ykoiso/src/A059_Goldfish.java new file mode 100644 index 0000000..77cc491 --- /dev/null +++ b/ykoiso/src/A059_Goldfish.java @@ -0,0 +1,62 @@ +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.solveMaxBeautyPoint(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 solveMaxBeautyPoint(final int poisStrength) { + //動的計画法(Dynamic Programming) + //途中経過の記憶 + int[][] dynamicPg = new int[num + 1][poisStrength + 1]; + // 最大値の記憶 + int maxBeautyPoint = 0; + // ある大きさに対して価値が大きくなる方を記憶する + //繰り返して最大値を計算 + for (int i = 0; i < num; i++) { + for (int j = 0; j <= poisStrength; j++) { + dynamicPg[i + 1][j] = Math.max(dynamicPg[i + 1][j], dynamicPg[i][j]); + if (j + weight[i] < poisStrength) { + dynamicPg[i + 1][j + weight[i]] = Math.max(dynamicPg[i + 1][j + weight[i]], + dynamicPg[i][j] + beauty[i]); + maxBeautyPoint = Math.max(maxBeautyPoint, dynamicPg[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 0000000..fa6a370 --- /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.solveMaxBeautyPoint(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.solveMaxBeautyPoint(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.solveMaxBeautyPoint(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.solveMaxBeautyPoint(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.solveMaxBeautyPoint(poisStrength); + final int expected = 0; + assertThat(actual, is(expected)); + } +} \ No newline at end of file -- GitLab From 5aed45135b4335021debc61ac651a50f52b37398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=A4=92=20=E8=80=80=E5=B9=B3?= Date: Tue, 23 Aug 2022 13:52:24 +0900 Subject: [PATCH 2/2] =?UTF-8?q?A059=20=E9=87=91=E9=AD=9A=E3=81=AE=E7=BE=8E?= =?UTF-8?q?=E3=81=97=E3=81=95=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/A059_Goldfish.java | 26 ++++++++++++++------------ ykoiso/test/A059_GoldfishTest.java | 10 +++++----- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ykoiso/src/A059_Goldfish.java b/ykoiso/src/A059_Goldfish.java index 77cc491..833efc2 100644 --- a/ykoiso/src/A059_Goldfish.java +++ b/ykoiso/src/A059_Goldfish.java @@ -2,8 +2,6 @@ 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(); @@ -21,7 +19,7 @@ public class A059_Goldfish { // Goldfishクラスのコンストラクタの呼び出し Goldfish goldfish = gf.new Goldfish(goldfishsWeight, goldfishsBeauty, goldfishNum); // ポイを壊さずに得られる最大の美しさを出力 - System.out.println(goldfish.solveMaxBeautyPoint(poisStrength)); + System.out.println(goldfish.calcMaxBeautyPoint(poisStrength)); scan.close(); } @@ -38,21 +36,25 @@ public class A059_Goldfish { } // ポイを壊さずに得られる最大の美しさを計算 - public int solveMaxBeautyPoint(final int poisStrength) { - //動的計画法(Dynamic Programming) - //途中経過の記憶 - int[][] dynamicPg = new int[num + 1][poisStrength + 1]; + 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++) { - dynamicPg[i + 1][j] = Math.max(dynamicPg[i + 1][j], dynamicPg[i][j]); + dynamicProgrammingBeautyPoint[i + 1][j] = + Math.max(dynamicProgrammingBeautyPoint[i + 1][j], + dynamicProgrammingBeautyPoint[i][j]); if (j + weight[i] < poisStrength) { - dynamicPg[i + 1][j + weight[i]] = Math.max(dynamicPg[i + 1][j + weight[i]], - dynamicPg[i][j] + beauty[i]); - maxBeautyPoint = Math.max(maxBeautyPoint, dynamicPg[i + 1][j + weight[i]]); + 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]]); } } } diff --git a/ykoiso/test/A059_GoldfishTest.java b/ykoiso/test/A059_GoldfishTest.java index fa6a370..ef8e1c5 100644 --- a/ykoiso/test/A059_GoldfishTest.java +++ b/ykoiso/test/A059_GoldfishTest.java @@ -12,7 +12,7 @@ public class A059_GoldfishTest { 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.solveMaxBeautyPoint(poisStrength); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); final int expected = 41; assertThat(actual, is(expected)); } @@ -21,7 +21,7 @@ public class A059_GoldfishTest { 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.solveMaxBeautyPoint(poisStrength); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); final int expected = 83; assertThat(actual, is(expected)); } @@ -30,7 +30,7 @@ public class A059_GoldfishTest { 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.solveMaxBeautyPoint(poisStrength); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); final int expected = 44; assertThat(actual, is(expected)); } @@ -39,7 +39,7 @@ public class A059_GoldfishTest { 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.solveMaxBeautyPoint(poisStrength); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); final int expected = 0; assertThat(actual, is(expected)); } @@ -48,7 +48,7 @@ public class A059_GoldfishTest { 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.solveMaxBeautyPoint(poisStrength); + final int actual =goldfish.calcMaxBeautyPoint(poisStrength); final int expected = 0; assertThat(actual, is(expected)); } -- GitLab