문제
In a land far, far away, three beautiful princes are trying to win the hand of a beautiful princess.
The princess, alas, wants to make sure that she chooses the prince with the greatest dowry, but she also wants to keep one of the princes from feeling too good about himself.
So she decides to marry the prince whose dowry is in the middle of the three.
Write a program to find the amount of the middle dowry.
해석
세 개의 서로 다른 정수가 한 줄에 하나씩 주어지면, 중간 크기의 정수를 출력하는 문제이다.
풀이 1: max()와 min() 제거
import sys
lines = sys.stdin.read().strip().split('\n')
intList = list(map(int, lines))
def findMiddleNum():
intList.remove(max(intList))
intList.remove(min(intList))
return intList[0]
print(findMiddleNum())
풀이 2: 정렬 후 중간값 반환
import sys
lines = sys.stdin.read().strip().split('\n')
intList = list(map(int, lines))
intList.sort()
print(intList[1])
비교 요약
방식 | 실행 시간 | 메모리 사용 | 장점 | 단점 |
remove() | 32ms | 32412KB | 직관적이고 구현이 간단함 | 중복 값이 있으면 안전하지 않음 |
sort() | 32ms | 32412KB | 코드가 간결하고 중복에 강함 | 데이터가 많으면 정렬 연산에 시간이 걸림 |
'Study > Algorithm' 카테고리의 다른 글
[백준] 2475번: 검증수 (0) | 2025.04.24 |
---|---|
[백준] 2438번: 별 찍기 - 1 (0) | 2025.04.24 |
[항해 알고리즘 스터디] 2주차 과제 정리 (후위 표기법 계산하기 / 중복 없이 가장 긴 문자열 찾기) (2) | 2025.04.24 |
[항해 알고리즘 스터디] 1주차 실강 과제 (패션왕 신혜빈) (2) | 2025.04.18 |
[항해 알고리즘 스터디] 1주차 과제 정리 (팰린드롬 / 짝수·홀수) (0) | 2025.04.15 |