알고리즘 오답노트 10 (백준 - 1011)

박경준·2021년 6월 15일
0

알고리즘 문제풀이

목록 보기
11/24

Fly me to the Alpha Centauri

손으로 직접 수열을 적어가며 규칙을 찾는것이 포인트이다.

  • 편의상 거리의 제곱근이 1.5보다 작으면 1.4로, 1.5보다 크면 1.6으로 적었다.
거리거리의 제곱근횟수
111
21.42
31.63
423
52.44
62.44
72.65
82.65
935
103.46
113.46
123.46
133.67
143.67
153.67
1647
174.48
184.48
194.48
204.48
214.69
  1. 거리의 제곱근이 정수면 횟수는 2n-1.
  2. 거리의 제곱근의 소수부가 0.5보다 작으면 2 X (거리의 제곱근의 정수부)
  3. 거리의 제곱근의 소수부가 0.5보다 크면 2 X (거리의 제곱근의 정수부) + 1
import math

num = int(input())
result = []
for i in range(num):
  start, finish = map(int, input().split())
  distance = finish - start
  
  sqrt_distance = math.sqrt(distance)
  if sqrt_distance - int(sqrt_distance) > 0.5:
    result.append(2 * int(sqrt_distance) + 1)
  elif sqrt_distance == int(sqrt_distance):
    result.append(2 * int(sqrt_distance) -1)
  else:
    result.append(2 * int(sqrt_distance))
    
for i in result:
  print(int(i))
profile
빠굥

0개의 댓글