카테고리 없음

백준 1182 | 부분 수열의 합

손을 뻗어 하늘에 물감을 묻혀보자 2025. 4. 8. 08:59

 

백준 문제 1182

 

https://www.acmicpc.net/problem/1182

 

 

 

파이썬의 combination 이란 아주 근사한 tool을 사용하면

매우 쉽게 풀리는 문제이다.

 

combination 함수를 사용하면 알아서 모든 조합을 보여준다.

 

해당 tool 을 이용해 for loop 에 중첩을 걸어,

조건문 if 절을 걸면 끝.

 

 

# import combinations
from itertools import combinations

# receive the input
n, s = map(int, input().split())
numbers = list(map(int, input().split()))

# answer
answer = 0

for i in range(1, n + 1):
    for j in combinations(numbers, i):
        total = sum(j)
        if total == s:
            answer += 1
            
# print out the answer
print(answer)

 

 

 

이상!