BOJ::2629 양팔저울

https://www.acmicpc.net/problem/2629


1. 추를 비교하여 얻을 수 있는 모든 경우의 수에 대해 벡터에 저장

2. 저장된 벡터와 입력된 구슬의 무게 체크하여 Y, N 출력


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <vector>
using namespace std;
 
int abs(int a) {
    return (a > 0) ? a : -a;
}
 
int main() {
    int n, m;
    scanf("%d"&n);
    vector<int> a;
    vector<bool> check(40001,false);
    
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d"&x);
        int sz = a.size(); // 현재 a배열의 사이즈 저장
 
        // x가 있으면 x를 넣진 않지만
        // 중복되는 숫자가 있는 경우도 있을 수 있으므로
        // 아래 for문으로는 넘어가도록 해야함
        if (!check[x]) {
            a.push_back(x);
            check[x] = true;
        }
 
        // 현재까지 저장되어 있는 수들에 대해
        // 뺄셈과 덧셈하여 중복되지 않는경우 벡터에 추가
        for (int j = 0; j < sz; j++) {
            if (!check[abs(a[j] - x)]) {
                check[abs(a[j] - x)] = true;
                a.push_back(abs(a[j] - x));
            }
            if (!check[a[j] + x]) {
                check[a[j] + x] = true;
                a.push_back(a[j] + x);
            }
        }
    }
    
    scanf("%d"&m);
    while (m--) {
        int x;
        scanf("%d"&x);
        if (check[x]) printf("Y ");
        else printf("N ");
    }
    printf("\n");
}
cs


'BOJ::문제풀이' 카테고리의 다른 글

14612 김식당  (0) 2018.02.17
2468 안전영역  (0) 2018.02.17
1719 택배  (0) 2018.02.14
10816 숫자카드2  (0) 2018.02.12
1647 도시 분할 계획  (0) 2018.02.12

+ Recent posts