-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbj_14888.cpp
More file actions
73 lines (61 loc) · 1.47 KB
/
bj_14888.cpp
File metadata and controls
73 lines (61 loc) · 1.47 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 연산자 끼워넣기
#include <iostream>
#define MAX 11
using namespace std;
int nums[MAX];
int opers[MAX]; // 연산자들이 들어갈 순열
int opersPermutation[MAX];
bool visited[MAX] = {false, };
int oper[4];
int maxAns = -2e9;
int minAns = 2e9;
int calc(int n, int curOpers[]) {
int result = nums[0];
for(int i=0; i<n-1; i++) {
if(curOpers[i] == 0) {
result += nums[i+1];
} else if(curOpers[i] == 1) {
result -= nums[i+1];
} else if(curOpers[i] == 2) {
result *= nums[i+1];
} else if(curOpers[i] == 3) {
result /= nums[i+1];
}
}
if(result > maxAns) maxAns = result;
if(result < minAns) minAns = result;
return result;
}
void calcPermutation(int depth, int n) {
if(depth == n-1) {
calc(n, opersPermutation);
}
for(int i=0; i<n-1; i++) {
if(!visited[i]) {
visited[i] = true;
opersPermutation[depth] = opers[i];
calcPermutation(depth+1, n);
visited[i] = false;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=0; i<n; i++) {
cin >> nums[i];
}
int idx = 0;
for(int i=0; i<4; i++) {
cin >> oper[i];
for(int j=0; j<oper[i]; j++) {
opers[idx++] = i;
}
}
calcPermutation(0, n);
cout << maxAns << "\n" << minAns << "\n";
cout << "\n";
}