CodingTest/Content

백준 2798번 블랙잭 풀이 및 정답(python)

코딩손 2025. 2. 8. 13:55

문제 설명 및 풀이

주어진 카드 뭉치 중 3장을 찾고 목표하는 숫자보다 작거나 같으면서 최대한 큰 조합을 찾는 문제입니다.

 

모든 경우의 수를 다 찾아봐야하기 때문에 가장 쉽게는 3중 for문으로 풀 수 있습니다.

파이썬의 경우 내장함수인 combinations를 통해 3장의 조합을 찾을 수 있습니다.



정답 예시 코드

from itertools import combinations

# 입력
n, m = map(int, input().split())
card_list = list(map(int, input().split()))

# 3장 중 m 보다 작으면서 가장 큰 조합 찾기
sum_value = 0
for three_card in combinations(card_list, 3):
  if sum(three_card) <= m:
    sum_value = max(sum_value, sum(three_card))

# 출력
print(sum_value)