[BOJ][Python]일곱 난쟁이 #2309

MEIN_FIGUR·2021년 7월 28일
0

백준_문제풀이

목록 보기
11/21

https://www.acmicpc.net/problem/2309

📌풀이


내가 쓴 풀이(성공)

from itertools import combinations

small = []
for case in range(9):
    small.append(int(input()))
cases = list(combinations(small, 7))

for case in cases:
    if sum(case) == 100 :
        result = list(case)
        break
result.sort()
for r in result:
    print(r)
  • itertools모듈의 combinations를 통해 조합 활용
  • 9명의 난쟁이 중 7명을 뽑아, 합이 100이 되는 경우를 확인
  • 튜플의 형태를 리스트로 바꾸고, sort
  • 각 원소를 출력



📌후기


처음에 combinations를 활용할 때 뺄 2명을 뽑는 경우를 고려하였는데, 7명을 뽑는 경우와 경우의 수가 같아서 7명으로 하였다. combinations를 리스트로 바꾸어 저장할 때 튜플이라는 사실을 몰라서 몇번 틀렸었다!

profile
Growing Developer

0개의 댓글