Guest User

Untitled

a guest
Feb 25th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. /*
  2. * 1182. 부분집합의 합
  3. * N개의 정수로 이루어진 집합이 있을 때, 이 집합의 공집합이 아닌 부분집합 중에서 그 집합의 원소를 다 더한 값이 S가 되는 경우의 수를 구하는 프로그램을 작성하시오.
  4. * [입력]
  5. * 첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1<=N<=20, |S|<=1,000,000)
  6. * 둘쨰 줄에 N개의 정수가 빈칸을 사이에 두고 주어진다.
  7. * 주어지는 정수의 절대값은 100,000을 넘지 않는다.
  8. * 같은 수가 여러번 주어질 수도 있다.
  9. * [출력]
  10. * 첫째 줄에 합이 S가 되는 부분집합의 개수를 출력한다.
  11. */
  12. import java.util.*;
  13. public class Main {
  14. static Scanner scan = new Scanner(System.in);
  15. static int n = scan.nextInt();
  16. static int s = scan.nextInt();
  17. static int sum = 0;
  18. public static void main(String[] args) {
  19. // TODO Auto-generated method stub
  20. int[] arr = new int[n];
  21.  
  22. for(int i=0; i<n; i++) {
  23. arr[i] = scan.nextInt();
  24. }
  25. int output = function(arr, 0, sum);
  26. if(s==0) {
  27. output--;
  28. }
  29. System.out.println(output);
  30.  
  31. }
  32. private static int function(int[] arr, int i, int sum) {
  33. // TODO Auto-generated method stub
  34. if(i==arr.length) {
  35. if(sum==s) {
  36. return 1;
  37. } else {
  38. return 0;
  39. }
  40. }
  41. return function(arr, i+1, sum+arr[i])+function(arr,i+1, sum);
  42. }
  43.  
  44. }
Add Comment
Please, Sign In to add comment