This file contains hidden or 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
import sys | |
from itertools import combinations | |
n, s = map(int, input().split()) | |
arr = list(map(int, sys.stdin.readline().split())) | |
count = 0 | |
for i in range(1, n+1): | |
comb = list(combinations(arr, i)) | |
for c in comb: | |
if sum(c) == s: | |
count += 1 | |
comb.clear() | |
print(count) |
This file contains hidden or 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
import sys | |
from itertools import combinations | |
n = int(input()) | |
arr = list(map(int, sys.stdin.readline().split())) | |
s = set() | |
for i in range(1, n+1): | |
comb = list(combinations(arr, i)) | |
for c in comb: | |
s.add(sum(c)) | |
i = 0 | |
while True: | |
i += 1 | |
if i not in s: | |
print(i) | |
break |
combinations(arr, num)는 arr에 조합을 뽑을 리스트를 넣어주고, num에 조합의 크기를 적어주면 된다. 그 자체로는 <itertools.combinations object at 0x7fb667ec8ef0>와 같이 객체를 반환하므로 list() 안에 넣어서 리스트 형태로 사용해야 한다.
'코딩테스트 준비' 카테고리의 다른 글
재귀 + dp + 조합 공식 (0) | 2021.02.24 |
---|---|
서로소 집합 알고리즘 (0) | 2021.02.24 |
큐 자료구조 문제 (0) | 2021.02.23 |
DP - 중 (0) | 2021.02.22 |
가장 긴 증가하는 부분 수열 (LIS) (0) | 2021.02.21 |