코딩테스트 연습/백준

[백준] 1011번 : Fly me to the Alpha Centauri in python 설명

슈퍼짱짱 2019. 9. 3. 08:00
반응형

백준 1011번 알고리즘


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




코드1


1
2
3
4
5
6
7
8
9
for c in range(int(input())) :
    x,y=map(int,input().split())
    X=y-x;i=2;s=X
    if X<=2print(X);continue
 
    I=[];S=[]
    while s>0:s-=i;S.append(s);i+=2;I.append(i)
 
    print(I[len(I)-2]-1 if S[len(S)-2]<=I[len(I)-2]/2 else I[len(I)-2])
cs



코드설명


X를 이동해야 하는 총 거리. 즉, input x,y에 대해 y-x라 할 때,

X에 따른 출력값은 다음과 같다.


X

output 

description 

 S[len(S)-2]

 I[len(I)-2]

 

 

1+1 

 

 

1+1+1 

1+2+1 

1+2+1+1 

1+2+2+1 

1+2+2+1+1 

1+2+2+2+1 

1+2+3+2+1 

10 

1+2+3+2+1+1 

11 

1+2+3+2+2+1 

12 

1+2+3+3+2+1 

13 

... 

14 

 

15 

 

16 

 

17 

 

18 

 

19 

 

20 

 

21

 

... 

... 

 

 

 




코드2


1
2
3
for _ in range(int(input())):
    x,y=map(int,input().split());X=int((y-x-1)**0.5)
    print(2*X+1 if y-x>X**2+else 2*X)
cs




코드설명


y-x

X**2 + X 

output 

10 

12 

11 

12 

12 

12 

13 

12 

14 

12 

15 

12 

16 

12 

17 

20 

18 

20 

19 

20 

20 

20 

21 

20 

 ... 

 

 

... 









반응형