티스토리 뷰

import math, sys

for _ in range(int(input())):
    x, y = map(int, sys.stdin.readline().split())
    distance = y - x
    root = math.floor(math.sqrt(distance))
    if distance <= 3:
        print(distance)
    elif root ** 2 == distance:
        print(root * 2 - 1)
    elif root ** 2 + root < distance:
        print(root * 2 + 1)
    else:
        print(root * 2)

0 ≤ x < y < 2^31이므로 sys.stdin.readline()을 이용했다. 

규칙을 머리로는 찾을 수 없어서... 먼저 손으로 구해보았다. 다음과 같은 결과를 얻을 수 있었다.

거리가 3 이하인 경우는 거리를 출력한다.
거리의 제곱근이 정수인 경우는 제곱근*2 - 1을 출력한다.
거리가 제곱근을 내림한 것의 제곱과 제곱근의 합보다 큰 경우는 제곱근*2 + 1을 출력한다.
거리가 제곱근을 내림한 것의 제곱과 제곱근의 합보다 작은 경우는 제곱근*2를 출력한다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/04   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
글 보관함