sw사관학교 정글 2기/01 기초,재귀,완전탐색, 정렬
백준 1085번 -곱셉- with.Python3
금의야행
2021. 8. 7. 21:20
https://www.acmicpc.net/problem/1085
1085번: 직사각형에서 탈출
한수는 지금 (x, y)에 있다. 직사각형은 각 변이 좌표축에 평행하고, 왼쪽 아래 꼭짓점은 (0, 0), 오른쪽 위 꼭짓점은 (w, h)에 있다. 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램
www.acmicpc.net
문제
내 풀이
x, y, w, h = map(int, input().split())
dif1 = w-x
dif2 = h-y
if dif1 > x:
shortcut1 = x
else:
shortcut1 = dif1
if dif2 > y:
shortcut2 = y
else:
shortcut2 = dif2
if shortcut1 > shortcut2:
print(shortcut2)
else:
print(shortcut1)
해설
상당히 비효율적으로 짠듯. 가시성을 중요시 생각하긴함.
정답 풀이
x, y, w, h = map(int, input().split())
print(min(x, y, w-x, h-y))
출처:https://ooyoung.tistory.com/102
해석
min(), max() 등의 python 내장 함수 등장.
min함수는 min(iterable) 형태로 사용한다. 괄호( ) 안에 문자열, 리스트와 같은 반복 가능한 iterable 자료형을 입력하면 요소들 중 최솟값을 반환한다.