백준 1182 부분수열의 합

솜솜이·2023년 4월 4일
0

백준 알고리즘

목록 보기
7/10

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static int[] arr;
    private static int a, b;
    private static int res = 0;

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        a = Integer.parseInt(st.nextToken());
        b = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        arr = new int[a];
        for (int i = 0; i < a; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }
        dfs(0, 0);
        if (b == 0) {
            res--;
            System.out.println(res);
        } else {
            System.out.println(res);
        }
    }

    private static void dfs(int total, int idx) {
        if (idx == a) {
            if (total == b) res++;
            return;
        }
        dfs(total + arr[idx], idx + 1);
        dfs(total, idx + 1);
    }
}

재귀를 통해서 모든 경우의 수를 구하여 풀 수 있다.

구하는 값이 0일 경우에는 -1를 해야한다.

0개의 댓글