From e2f6a5172655e5798f9d69d1845cf61dc489e656 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: Thu, 28 Jul 2022 11:07:13 +0900 Subject: [PATCH 1/2] =?UTF-8?q?B006=20=E3=83=80=E3=83=BC=E3=83=84=E3=82=B2?= =?UTF-8?q?=E3=83=BC=E3=83=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B006_Darts.java | 68 +++++++++++++++++++++++++++++++++ ykoiso/test/B006_DartsTest.java | 24 ++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 ykoiso/src/B006_Darts.java create mode 100644 ykoiso/test/B006_DartsTest.java diff --git a/ykoiso/src/B006_Darts.java b/ykoiso/src/B006_Darts.java new file mode 100644 index 0000000..3732009 --- /dev/null +++ b/ykoiso/src/B006_Darts.java @@ -0,0 +1,68 @@ +import java.util.Scanner; +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class B006_Darts { + public static final double G = 9.8; + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + boolean isHit = false; + // 初期地点の高さ + final double originY = scan.nextInt(); + // 初速 + final double velosity = scan.nextInt(); + // 角度 + final double angle = scan.nextInt(); + // 的までの距離 + final double disatance = scan.nextInt(); + // 的の高さ + final double targetY = scan.nextInt(); + // 的の半径 + final double targetRadius = scan.nextInt() / 2; + // 矢の最終地点の高さ + final double lastY = calcLandingPlace(originY, velosity, angle, disatance); + // 当たるかの判定 + isHit = judgeHit(lastY, targetY, targetRadius); + showAnswer(isHit, lastY, targetY); + scan.close(); + } + + public static double calcLandingPlace(final double originY, final double velosity, + final double angle, final double disatance) { + // 度数で渡されるのでラジアン表記に直している + final double rad = Math.toRadians(angle); + final double child = G * Math.pow(disatance, 2); + final double mother = 2 * Math.pow(velosity, 2) * Math.pow(Math.cos(rad), 2); + final double lastY = originY + (disatance * Math.tan(rad)) - (child / mother); + return lastY; + } + + // 当たるかの判定 + // テスト用 + public static boolean judgeHit(final double lastY, final double targetY, + final double targetRadius) { + // 的との誤差 + final double errorAbsDistance = Math.abs(targetY - lastY); + if (errorAbsDistance <= targetRadius) { + return true; + } else { + return false; + } + } + + public static void showAnswer(final boolean isHit, final double lastY, final double targetY) { + // 的との誤差 + final double errorAbsDistance = Math.abs(targetY - lastY); + // 誤差、四捨五入用 + BigDecimal errorDistance = new BigDecimal(errorAbsDistance); + errorDistance = errorDistance.setScale(1, RoundingMode.HALF_EVEN); + // 出力 + if (isHit) { + System.out.print("Hit "); + System.out.println(errorDistance); + } else { + System.out.println("Miss"); + } + } +} diff --git a/ykoiso/test/B006_DartsTest.java b/ykoiso/test/B006_DartsTest.java new file mode 100644 index 0000000..7074a56 --- /dev/null +++ b/ykoiso/test/B006_DartsTest.java @@ -0,0 +1,24 @@ +import static org.junit.Assert.assertThat; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.*; + +public class B006_DartsTest { + @Test + public void 的に当たると判断できる() { + B006_Darts cut = new B006_Darts(); + final double lastY = cut.calcLandingPlace(10, 13, 45, 20); + final boolean actual = cut.judgeHit(lastY, 10, 10); + final boolean expected = true; + assertThat(actual, is(expected)); + } + + @Test + public void 的に当たらないと判断できる() { + B006_Darts cut = new B006_Darts(); + final double lastY = cut.calcLandingPlace(10, 15, 45, 10); + final boolean actual = cut.judgeHit(lastY, 10, 5); + final boolean expected = false; + assertThat(actual, is(expected)); + } +} -- GitLab From 2ad26732c2a1bb3886c9f7476dfa468162110e1e 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: Thu, 28 Jul 2022 16:26:56 +0900 Subject: [PATCH 2/2] =?UTF-8?q?B006=20=E3=83=80=E3=83=BC=E3=83=84=E3=82=B2?= =?UTF-8?q?=E3=83=BC=E3=83=A0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ykoiso/src/B006_Darts.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ykoiso/src/B006_Darts.java b/ykoiso/src/B006_Darts.java index 3732009..d9b7cc0 100644 --- a/ykoiso/src/B006_Darts.java +++ b/ykoiso/src/B006_Darts.java @@ -7,7 +7,6 @@ public class B006_Darts { public static void main(String[] args) { Scanner scan = new Scanner(System.in); - boolean isHit = false; // 初期地点の高さ final double originY = scan.nextInt(); // 初速 @@ -23,7 +22,7 @@ public class B006_Darts { // 矢の最終地点の高さ final double lastY = calcLandingPlace(originY, velosity, angle, disatance); // 当たるかの判定 - isHit = judgeHit(lastY, targetY, targetRadius); + final boolean isHit = judgeHit(lastY, targetY, targetRadius); showAnswer(isHit, lastY, targetY); scan.close(); } @@ -55,8 +54,8 @@ public class B006_Darts { // 的との誤差 final double errorAbsDistance = Math.abs(targetY - lastY); // 誤差、四捨五入用 - BigDecimal errorDistance = new BigDecimal(errorAbsDistance); - errorDistance = errorDistance.setScale(1, RoundingMode.HALF_EVEN); + final BigDecimal errorDistance = + new BigDecimal(errorAbsDistance).setScale(1, RoundingMode.HALF_EVEN); // 出力 if (isHit) { System.out.print("Hit "); -- GitLab