주어진 9명의 키에서 7명을 골라 합이 100이 되는 경우를 찾고, 이를 오름차순으로 출력하는 문제이다.
for문을 사용하여 9명 중 2명을 선택combinations(9명, 7)으로 7명의 조합을 찾기for문을 이용한 풀이import sys
heights = [int(input()) for _ in range(9)]
total_height = sum(heights)
for i in range(9): # 첫 번째 난쟁이 제외
for j in range(i + 1, 9): # 두 번째 난쟁이 제외
if total_height - (heights[i] + heights[j]) == 100:
result = [heights[k] for k in range(9) if k != i and k != j] # 두 명을 제외한 리스트 생성
result.sort() # 오름차순 정렬
for h in result:
print(h)
sys.exit() # 정답을 찾으면 프로그램 종료
combinations)을 이용한 풀이from itertools import combinations
heights = [int(input()) for _ in range(9)]
total_height = sum(heights)
for exclude in combinations(heights, 2): # 2명 조합을 생성
if total_height - sum(exclude) == 100:
result = sorted([h for h in heights if h not in exclude])
for h in result:
print(h)
break
sys.exit() 사용: 정답을 찾으면 즉시 종료하여 불필요한 반복을 방지sys.stdin.read 사용 가능: 여러 줄 입력을 빠르게 받을 수 있음