백준 1011 파이썬

김덕현·2021년 7월 20일
1

BAEKJOON

목록 보기
59/62

1.문제

https://www.acmicpc.net/problem/1011

2.코드

import math
t=int(input())
for _ in range(t):
    x,y=map(int,input().split())
    d=y-x
    n=math.floor(math.sqrt(d))
    n_pow=math.pow(n,2)
    if d<3:
        print(d)
    elif d>n_pow and d<=n_pow+n:
        print(n*2)
    elif d>n_pow+n and d<=math.pow(n_pow+1,2):
        print(n*2+1)
    elif d==n_pow:
        print(n*2-1)

3.코드해설

제곱수마다 Route에 제곱근이 되는 수가 추가된다.
n은 거리의 제곱근의 내림, 예를 들면 d가 10일때 3.xx이므로 3이된다.
n_pow는 거리 d보다 작은 제곱수를 나타낸다. 예를 들면 9~15까지의 n_pow는 '9'이다.

if d<3:
        print(d)

거리가 3이 안된다면 거리만큼의 count가 들어가기에 d가 출력된다.

elif d>n_pow and d<=n_pow+n:
        print(n*2)

'제곱수'보다 크고 '제곱수+제곱근'보다 작거나 같다면 n*2를 출력한다.

elif d>n_pow+n and d<=math.pow(n_pow+1,2):
        print(n*2+1)

'제곱수+제곱근'보다 크고 다음 제곱수보다 작거나 같다면 n*2+1을 출력한다.

elif d==n_pow:
        print(n*2-1)

거리 d가 제곱수이면 n*2-1를 출력한다.

위의 네 가지의 경우로 쪼게면 쉽게 풀 수 있는 문제이다.

profile
BAEKJOON STUDYING

0개의 댓글