#문제
2798번: 블랙잭
첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장
www.acmicpc.net
#풀이 & 학습한 내용
"세 장의 카드를 고르는 모든 경우를 고려하는 문제"입니다. 문제를 풀 때, python의 itertools를 이용하면 순열과 조합을 for문 없이 쉽게 구현할 수 있습니다.(velog.io/@dramatic/Python-permutation-combination-%EC%88%9C%EC%97%B4%EA%B3%BC-%EC%A1%B0%ED%95%A9)
#소스코드 (5번째 줄 주목)
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import itertools
N,M = map(int,input().split())
cards = list(map(int,input().split()))
cards = list(itertools.combinations(cards,3)) #nC3 조합
summ=-1
for card in cards:
if summ<sum(card) and sum(card)<=M:
summ=sum(card)
print(summ)
|
cs |
github.com/HoYoungChun/Algorithm_PS/blob/master/Brute-force/BOJ_2798.py
'Algorithm > Brute-force' 카테고리의 다른 글
| [Python] 백준 7568번 : 덩치 (S5) - 브루트포스단계별3 (0) | 2021.01.28 |
|---|---|
| [Python] 백준 2231번 : 분해합 (B2) - 브루트포스단계별2 (0) | 2021.01.28 |
| [C++] 백준 2798번 : 거꾸로 구구단 (B2) (0) | 2020.07.15 |
| [C++] 백준 4641번 : Doubles (B1) (0) | 2020.07.15 |
| [C++] 백준 1065번 : 한수 (S4) (0) | 2020.07.15 |