0 = +

1 = -

2 = *

3 = /

주어진 갯수만큼 배열에 넣고 next_permutation으로 순열 만들어가면서 모두 해보면 된다.

아니면 dfs(+,-,*,/)으로도 가능.


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
#include <stdio.h>
#include <algorithm>
using namespace std;
 
int n, a[12], b[12], maxv = -2e9, minv = 2e9;
 
int main() {
    scanf("%d"&n);
    for (int i = 0; i < n; i++scanf("%d"&a[i]);
    for (int i = 0, cn = 0, t; i < 4; i++) {
        scanf("%d"&t);
        while (t--) b[cn++= i;
    }
    do {
        int sum = a[0];
        for (int i = 1; i < n; i++) {
            if (b[i - 1== 0) sum += a[i];
            else if (b[i - 1== 1) sum -= a[i];
            else if (b[i - 1== 2) sum *= a[i];
            else sum /= a[i];
        }
        minv = min(minv, sum);
        maxv = max(maxv, sum);
    } while (next_permutation(b, b + n - 1));
    printf("%d\n%d", maxv, minv);
    return 0;
}
cs


'BOJ' 카테고리의 다른 글

14890 경사로  (0) 2017.10.26
14889 스타트와 링크  (0) 2017.10.26
14648 쿼리 맛보기  (0) 2017.10.03
8068 Water  (0) 2017.10.03
7579 앱  (0) 2017.10.03

+ Recent posts