Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 1182. 부분집합의 합
- * N개의 정수로 이루어진 집합이 있을 때, 이 집합의 공집합이 아닌 부분집합 중에서 그 집합의 원소를 다 더한 값이 S가 되는 경우의 수를 구하는 프로그램을 작성하시오.
- * [입력]
- * 첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1<=N<=20, |S|<=1,000,000)
- * 둘쨰 줄에 N개의 정수가 빈칸을 사이에 두고 주어진다.
- * 주어지는 정수의 절대값은 100,000을 넘지 않는다.
- * 같은 수가 여러번 주어질 수도 있다.
- * [출력]
- * 첫째 줄에 합이 S가 되는 부분집합의 개수를 출력한다.
- */
- import java.util.*;
- public class Main {
- static Scanner scan = new Scanner(System.in);
- static int n = scan.nextInt();
- static int s = scan.nextInt();
- static int sum = 0;
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] arr = new int[n];
- for(int i=0; i<n; i++) {
- arr[i] = scan.nextInt();
- }
- int output = function(arr, 0, sum);
- if(s==0) {
- output--;
- }
- System.out.println(output);
- }
- private static int function(int[] arr, int i, int sum) {
- // TODO Auto-generated method stub
- if(i==arr.length) {
- if(sum==s) {
- return 1;
- } else {
- return 0;
- }
- }
- return function(arr, i+1, sum+arr[i])+function(arr,i+1, sum);
- }
- }
Add Comment
Please, Sign In to add comment