Baekjoon 2798 Blackjack Python Solution

힘센캥거루
2025년 10월 7일(수정됨)
1
16

Think of the brute-force algorithm as just brute-forcing it.

We have plenty of time and memory — so what's the problem?

Since we pick 3 cards, just run three for-loops.

However, be careful about the condition of picking the same card.

Details are given below.

I'll omit a more detailed explanation.

# sys.stdin.readline을 이용해 데이터를 받는다.
from sys import stdin
put = stdin.readline

# 카드의 수, 그리고 목표 숫자를 받는다.
# 카드 숫자들은 map으로 쪼갠 뒤, list로 변환한다.
numOfCard, targetNum = map(int, put().split())
cardLst = list(map(int, put().split()))

# 총합들과 차이를 저장한다.
totals = []
diff = []

# 3장의 카드가 필요하므로 3번의 for문을 돈다.
# 카드리스트에서 카드를 한장씩 뽑는다.
for i in cardLst:
    for j in cardLst:
        for k in cardLst:
        
        	# 문제는 같은 카드를 뽑을 수 있다는 것.
            # 같은 카드를 뽑으면 넘어간다.
            if i == j or i == k or j == k:
                continue
                
            # 총합을 구한다.
            # 총합이 목표 숫자보다 크면 넘어간다.
            total = i + j + k
            if targetNum - total < 0 :
                continue
                
            # 조건을 모두 통과하면 총합과 차이값을 저장한다.
            totals.append(total)
            diff.append(targetNum - total)
            
# 차이값들 중 최솟값의 인덱스를 구한다. 
# totals에서 최솟값의 인덱스를 넣어 출력한다.
print(totals[diff.index(min(diff))])
Baekjoon 2798 Blackjack Python Solution-1

관련 글

Automating School Work – Using AI to Check Subject-Specific Remarks in Student Records
Automating School Work – Using AI to Check Subject-Specific Remarks in Student Records
If I had to pick the most meaningless, exhausting, and boring task at school, I would choose checking student records.In middle school, the student re...
Book Review and Challenge Review of Chapter 7 of *Building an LLM from Scratch*
Book Review and Challenge Review of Chapter 7 of *Building an LLM from Scratch*
Chapter 7 covers the process of fine-tuning a model to follow instructions.In other words, making it give the desired response to a given question.As...
Review of Chapter 6 of *Build an LLM from Scratch*
Review of Chapter 6 of *Build an LLM from Scratch*
Chapter 6 is about fine-tuning for classification.The example used is building a spam classifier.A spam classifier determines whether something is spa...
Review of Chapter 5 of *Building an LLM from Scratch*
Review of Chapter 5 of *Building an LLM from Scratch*
Today is December 14.The challenge period actually ended two weeks ago, but I couldn’t just give up on writing a review.Because these TILs I leave lik...
Impressions After Reading Chapter 4 of “LLM From Scratch”
Impressions After Reading Chapter 4 of “LLM From Scratch”
Today is November 26, so if I finish one chapter a day, I’ll complete the challenge.I’m not sure if I can do it with my first and second kids constant...
Review of Chapter 3 of Learning LLM from Scratch
Review of Chapter 3 of Learning LLM from Scratch
After spilling a bucket of water on my MacBook, I was in shock and wasted about 3-4 days. In retrospect, since my MacBook was already damaged, I should have thought of it as being sent for repair and done something. Anyway, although it's a bit late, I am determined to see it through and leave a review of Chapter 3. 1. Attention Mechanism Chapter 3...

댓글을 불러오는 중...