728x90
# 백준 브루트 포스 : 2798번 - 블랙잭
n,m = map(int, input().split())
num = list(map(int, input().split()))
res = 0
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
if num[i]+num[j]+num[k] <= m: # 3개의 수의 합이 m을 넘지 않는 경우
res = max(res, num[i]+num[j]+num[k]) # m에 최대한 가까운 카드의 합을 구함
else: # m을 넘은 경우
continue
print(res)
그냥 세 수의 합을 모두 검사하는 방법이다...
아래의 풀이방법은 combinations를 이용해서 3개의 수를 선택한 조합을 통해 구하는 법이다.
# combinations
# 백준 브루트 포스 : 2798번 - 블랙잭
from itertools import combinations
n,m = map(int, input().split())
num = list(map(int, input().split()))
res = 0
com = list(combinations(num, 3))
for i in com:
if sum(i) <= m:
res = max(res, sum(i))
print(res)'Algorithm > Baekjoon' 카테고리의 다른 글
| #백준 7586, 파이썬, 리스트 (0) | 2022.08.26 |
|---|---|
| #백준 2231, 파이썬, list, map, 깃허브 에러 (0) | 2022.07.21 |
| #백준 3053, 파이썬, 소수점 반올림, 소수점 자릿수 표현, f-string (0) | 2022.07.21 |
| #백준 3009, 파이썬, append, insert, count 함수 (0) | 2022.07.21 |
| #백준 1085, 파이썬, 삼항연산자, 절대값 abs (0) | 2022.07.21 |