ideal insane

Resistance ∙ Pioneer ∙ 생산자

Algorithm/BAEKJOON - Python

2798: 블랙잭

Idealinsane 2022. 12. 28. 20:56
728x90
 

2798번: 블랙잭

첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장

www.acmicpc.net

2798: 블랙잭

from itertools import combinations 
# 입력 값 받기
(N, M) = input().split()
card_box = input().split()

# 조합 함수를 활용하여 가능한 카드 3장의 조합 구하고 sum()으로 카드 3장의 합 구하기.
combination = list(combinations(card_box, 3))
sum_list = list()

for set in combination:
    set = list(map(int, set))
    sum_list.append(sum(set))

# M 보다 작거나 같은 합을 candidate 리스트에 넣는다. 
candidate = [x for x in sum_list if x <= int(M)]

# M을 넘지 않으면서 M에 최대한 가까운 최댓값 출력
print(max(candidate))

combinations, sum, max 등 함수를 활용하여 쉽게 해결할 수 있었습니다.

반응형

'Algorithm > BAEKJOON - Python' 카테고리의 다른 글

2231: 분해합  (0) 2023.01.01
1015: 수열 정렬 - 문제 이해/해설  (0) 2022.12.26
2525: 오븐 시계  (0) 2022.12.25
4673번: 셀프 넘버  (2) 2022.12.25