Python - [백준]2798-블랙잭

Paek·2023년 1월 22일
0

코테공부

목록 보기
2/44

문제

블랙잭 변형게임으로, 입력으로 주어진 N개의 카드중 3개를 그 총합이 M에 가깝게 골라 그 합을 리턴하는 문제이다.

접근방법

nC3, 즉 조합 문제이다. 파이썬에서는 조합을 지원하므로 Combination 함수를 사용하여 만든 후 그 합이 M 이하인것중 최대의 값을 찾아주었다.

import sys
from itertools import combinations

n, m = map(int, sys.stdin.readline().split())
arr = list(map(int, sys.stdin.readline().split()))
max_num = 0
result_arr = list(combinations(arr, 3))
for i in result_arr:
    if sum(i) <= m:
        max_num = max(max_num, sum(i))
print(max_num)
profile
티스토리로 이전했습니다. https://100cblog.tistory.com/

0개의 댓글