[랜덤 마라톤] 튕기는 볼링공(1876)

lhs's avatar
Dec 14, 2024
[랜덤 마라톤] 튕기는 볼링공(1876)
notion image
notion image

1. 문제 풀이 아이디어

  • 공이 가운데로 올 때 부딪힐 수 있는 최소 거리와 최대 거리를 계산한 뒤, 공이 이동할 때마다 그 위치가 해당 범위 안에 들어오는지 확인하면 문제를 해결할 수 있다.

2. 나의 정답 코드

public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder stringBuilder = new StringBuilder(); int n = Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i < n; i++) { StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine()); double t = Double.parseDouble(stringTokenizer.nextToken()); double x = Double.parseDouble(stringTokenizer.nextToken()); double min = t * 100 - (16 / Math.sin(Math.toRadians(x))); double max = t * 100 + (16 / Math.sin(Math.toRadians(x))); double move = 85 / Math.tan(Math.toRadians(x)); boolean hit = false; double total = 0; while (max > total) { if (total > min) { hit = true; break; } total += move; } stringBuilder.append(hit ? "yes" : "no").append("\n"); } System.out.print(stringBuilder); bufferedReader.close(); } }

3. 정리

  • t * 100 ± (16 / Math.sin(Math.toRadians(x)))를 이용해 부딪힐 수 있는 최소 거리와 최대 거리를 구한다.
  • 85 / Math.tan(Math.toRadians(x))를 이용해 공이 가운데로 이동할 때의 간격을 계산한다.
  • while문을 사용하여 공의 총 이동 거리가 최대 범위(max)보다 작을 때까지 반복하며, 총 이동 거리가 최소 범위(min)를 넘어가면 충돌한 것으로 간주하고 hittrue로 설정한 뒤 반복문을 종료한다.
  • 충돌 여부에 따라 "yes" 또는 "no"를 출력하여 문제를 해결한다.
Share article

LHS's Study Space