티스토리 뷰

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를 출력한다.
'BOJ' 카테고리의 다른 글
| [백준/BOJ] 11653번: 소인수분해(Python) (0) | 2021.04.01 |
|---|---|
| [백준/BOJ] 1978, 2581, 1929번: 소수(Python) (0) | 2021.04.01 |
| [백준/BOJ] 10757번: 큰 수 A+B(Python) (0) | 2021.04.01 |
| [백준/BOJ] 2839번: 설탕 배달(Python) (0) | 2021.04.01 |
| [백준/BOJ] 2775번: 부녀회장이 될테야(Python) (0) | 2021.03.31 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- valid annotation
- 선형 배열
- 직접 주입
- python flag
- 자료구조
- ljust
- sequence type
- airbnb clone
- postfix notation
- rjust
- Stack
- for-else
- 스프링부트
- most_common
- string module
- 양방향 연결 리스트
- 출력 형식 지정
- Django
- python
- 의존 주입
- springboot
- 파이썬
- ValidataionUtils
- divmod
- djnago
- 선형 탐색
- 이진 탐색
- initBinder
- tailwind
- 스택
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함
