-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cherry-pick from fb4bbea
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |