diff --git a/ykoiso/src/B006_Darts.java b/ykoiso/src/B006_Darts.java new file mode 100644 index 0000000000000000000000000000000000000000..d9b7cc0c86ba85efd740c90d7d49b989e33c429e --- /dev/null +++ b/ykoiso/src/B006_Darts.java @@ -0,0 +1,67 @@ +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); + // 初期地点の高さ + 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); + // 当たるかの判定 + final boolean 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); + // 誤差、四捨五入用 + final BigDecimal errorDistance = + new BigDecimal(errorAbsDistance).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 0000000000000000000000000000000000000000..7074a56f34ca4a30f4e943bf0b6497e638c8d86a --- /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)); + } +}