부분수열의 합
Updated:
package 백준;
import java.util.Scanner;
public class 부분수열의합 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
S = sc.nextInt();
arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < N; i++) {
sub_sum(i, 1, arr[i]);
}
System.out.println(count);
}
static int N;
static int S;
static int[] arr;
static int count;
static void sub_sum(int k, int cnt, int sum) {
if (sum == S)
count++;
if (cnt == N)
return;
for (int i = k + 1; i < N; i++) {
sub_sum(i, cnt + 1, sum + arr[i]);
}
}
}
Leave a comment