[BOJ] 14888 연산자 끼워넣기

Time Lapse :5min 2sec

14888.c

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
#include <stdio.h>
#define gc() getchar()
int nums[11];
int susicks[4];
int imax = -987654321, imin = 987654321, N;
int fRI() {
register int N = gc(), r = 0;
for (; 0x30>N || N>0x3A; N = gc());
do {
r = (r << 3) + (r << 1) + (N & 0b1111); N = gc();
} while (0x30 <= N);
return r;
}
void DFS(int idx, int cur) {
if (idx == N) {
imax = imax > cur ? imax : cur;
imin = imin > cur ? cur : imin;
return;
}
for (int i = 0; i < 4; ++i) {
if (susicks[i]) {
--susicks[i];
switch (i) {
case 0: DFS(idx + 1, cur + nums[idx]); break;
case 1: DFS(idx + 1, cur - nums[idx]); break;
case 2: DFS(idx + 1, cur * nums[idx]); break;
case 3: DFS(idx + 1, cur / nums[idx]); break;
}
++susicks[i];
}
}
}
int main(int argc, char** argv) {
N = fRI();
for (int i = 0; i < N; ++i)
nums[i] = fRI();
for (int i = 0; i < 4; ++i)
susicks[i] = fRI();
DFS(1, nums[0]);
printf("%d\n%d", imax, imin);

}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/14888/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.