알고리즘/백준

[백준 알고리즘] 1182번: 부분수열의 합 (Python / 파이썬)

gyujh 2024. 1. 24. 23:31
정답 코드
#1182번: 부분수열의 합

import sys

def find(pointer, total):
    global answer
    if pointer >= n:
        return
    find(pointer + 1, total) # 토탈에 현재 값이 반영되지 않음
    total += a[pointer]
    find(pointer + 1, total)  # 토탈에 현재 값 반영
    if total == s:
        answer += 1

n, s = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
answer = 0

find(0, 0)
print(answer)

 

1182번: 부분수열의 합

첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.

www.acmicpc.net