[백준/파이썬] 14530번 풀이
업데이트:
문제 정보
- 문제 출처: 백준 온라인 저지
- 문제 링크: 14530번 문제
- 문제풀이 코드 GitHub 링크
- 제출 언어: Python 3
풀이
문제
Farmer John has lost his prize cow Bessie, and he needs to find her!
| Fortunately, there is only one long path running across the farm, and Farmer John knows that Bessie has to be at some location on this path. If we think of the path as a number line, then Farmer John is currently at position (x) and Bessie is currently at position (y) (unknown to Farmer John). If Farmer John only knew where Bessie was located, he could walk directly to her, traveling a distance of ( | x - y | ). Unfortunately, it is dark outside and Farmer John can’t see anything. … |
입력 요약
The single line of input contains two distinct space-separated integers (x) and (y). Both are in the range (0 \ldots 1,000).
출력 요약
Print one line of output, containing the distance Farmer John will travel to reach Bessie.
코드
x,y=map(int,input().split())
s,c=0,1
while True:
if c<0 and y<=x and y>=x-abs(c) or c>0 and y>=x and y<=x+abs(c) :
s+=abs(x-y)
print(s)
break
c*=-2
s+=abs(c)
설명
핵심은 구현 관점에서 Farmer John has lost his prize cow Bessie, and he needs to find her! …를 만족하도록 로직을 구성하는 것입니다.
코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.
경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.
댓글남기기