공부/알고리즘문제
백준 1182 [비트 마스크] 부분집합의 합
Qdy
2018. 7. 29. 20:09
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.Scanner; public class BOJ_1182 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int result = sc.nextInt(); int[] arr = new int[n]; int bitMax = (int) (Math.pow(2, n) - 1); int cnt = 0; for (int i = 0; i < n; i++) { // Initialization arr[i] = sc.nextInt(); } for (int bit = 1; bit <= bitMax; bit++) { // 모든 비트 순회 int sum = 0; for (int i = 0; i < n; i++) { // 1인 비트 체크 if ((bit & (1 << i)) != 0) sum += arr[i]; } if (sum == result) cnt++; } System.out.println(cnt); } } | cs |