Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[Python] 세 정수의 중앙값 구하기 알고리즘 본문
def med3(a, b, c):
if a >= b: # 2 >= 5 -> False
if b >= c:
return b
elif a <= c:
return a
else:
return c
elif a > c: # 2 > 4 -> False
return a
elif b > c: # 5 > 4 -> True
return c # return 4
else:
return b
# 1, 2, 3 -> 2
# 2, 5, 4 -> 4
def med3(a, b, c):
if (b >= a and c <= a) or (b <= a and c >= a):
return a
elif (a > b and c < b) or (a < b and c > b):
return b
return c
2번 알고리즘이 짧지만 효율이 1보다 안좋다.
'백준' 카테고리의 다른 글
| [Python] 유니코드 ↔ 문자 변환 (ord, chr) (0) | 2023.02.27 |
|---|---|
| [Python] 소수 구하는 방법 (에라토스테네스의 체) (0) | 2023.02.22 |
| [Python] 정렬 (sort, sorted) (0) | 2023.02.16 |
| [Python] 시간 복잡도 🕒 (0) | 2022.12.26 |
| [Python] 숫자로 된 문자열을 정수형으로 변환 (0) | 2022.12.23 |
Comments