백준 #2798 블랙잭 (with python)

휴먼시아·2024년 8월 8일

Algorithm

목록 보기
2/5

백준 #2978번 블랙잭 (부르트포스)

난이도 브론즈2


문제

딜러는 N장의 카드를 고르고 플레이어는 그중 3장을 고른다.
3장의 합이 M보다 크지 않으며 가장 가까운 사람이 승리

N(3<=N<=100)
M(10<=M<=300,000)

코드1: for문

3중 for문을 사용하여 카드 3장을 고르는 방법


N, M = map(int, input().split())
cards = list(map(int, input().split()))

result=0
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            sum = cards[i] + cards[j] + cards[k]
            if sum <= M:
                result = max(result, sum)

print(result)

코드2: combonations

combinations을 사용하여 무작위로 카드 3장을 추출하는 방법

from itertools import combinations
N, M = map(int, input().split())
cards = list(map(int, input().split()))

result = 0  #결과값 초기화

for combo in combinations(cards, 3):
    if sum(combo) <=M:
        result = max(result, sum(combo))

print (result)

문제 출처

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





글로벌소프트웨어캠퍼스와 교보DTS가 함께 진행하는 챌린지입니다.

#내맘대로TIL챌린지 #교보DTS #클라우드교육 #글로벌소프트웨어캠퍼스 #GSC신촌

profile
코(딩)찔찔이 성장기

1개의 댓글

comment-user-thumbnail
2024년 8월 9일

8월부터 재도전!! 8월 챌린지 성공 가보자구용~!!!

답글 달기