척척석사가 되어보자

백준 1182번 본문

알고리즘/백준

백준 1182번

0.genius 2019. 9. 8. 19:32
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
#include <iostream>
#include <vector>
using namespace std;
 
int main(){
    int N, S;
    vector <int> a;
    cin >> N >> S ;
    int n2 = N;
    while(n2--){
        int num;
        cin >> num;
        a.push_back(num);
    }
    
    int n = 1<<N;   
    //-7 -3 -2 5 8  j1:i2번쨰 10:3번째 11:4번째
    //00001 00010 00011 00100 00101 00110 00111 01000
    int count =0;
    for(int i=1; i< n; i++){
        int sum = 0;
        for(int j=0; j< N; j++){
            if((i&(1<<j))!=0){
                sum+= a[N-j-1];   
            }
        }
        if(sum==S){
            count ++;
        }
    }
    cout << count << '\n';
    return 0;
}
cs

유의

1. 비트순서와 index순서 헷갈리지 않기 a[j] -> a[N-j-1] 로 바꿔줌

2. 반례 생각하기 i =0, sum =0 이면 그냥 count ++ 되잖아

'알고리즘 > 백준' 카테고리의 다른 글

백준 11052  (0) 2019.09.18
백준 2294 문제  (0) 2019.09.10
백준 2503번  (0) 2019.09.08