Skip to content

Commit

Permalink
update baekjoon_2798 as a post
Browse files Browse the repository at this point in the history
cherry-pick from fb4bbea
  • Loading branch information
djccnt15 committed Aug 11, 2023
1 parent a8e861e commit cdec60f
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions _posts/codingtest/2023-08-11-baekjoon_2798.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
published: true
layout: post
title: '[백준] 2798. 블랙잭'
description: >
백준 2798. 블랙잭
categories: [CodingTest]
tags: [baekjoon, python]
image:
path: /assets/img/posts/thumbnail_codingtest.png
related_posts:
- _posts/category/0000-01-01-format_post.md
---
* toc
{:toc}

## 문제

N장의 카드에 써져 있는 숫자가 주어졌을 때, M을 넘지 않으면서 M에 최대한 가까운 카드 3장의 합을 구해 출력하라.

## 풀이

[경우의 수](/statistics/statistics_05/#2-경우의-수)를 다룰 때 나오는 [조합](/statistics/statistics_05/#2-3-조합)에 대한 문제로, 아래와 같이 모든 경우의 수를 찾아낸 후 조건에 맞게 결과를 출력해주면 된다.

```python
(n, m), l = ([int(x) for x in input().split()] for _ in range(2))

print(
max(
x for x in (
l[a] + l[b] + l[c]
for a in range(n - 2)
for b in range(a + 1, n)
for c in range(b + 1, n)
) if x <= m
)
)
```

Python에는 [조합](/statistics/statistics_05/#2-3-조합)을 위한 내장 클래스 `combinations`가 있다. `combinations`를 사용하면 아래와 같이 간결하게 만들 수 있다.

```python
from itertools import combinations

(n, m), l = ([int(x) for x in input().split()] for _ in range(2))

print(max(x for x in (sum(x) for x in list(combinations(l, 3))) if x <= m))
```

---
## Reference
- [풀어낸 문제 git repository](https://github.com/djccnt15/coding_test)
- [문제 출처](https://www.acmicpc.net/problem/2798)

0 comments on commit cdec60f

Please sign in to comment.