-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14888.cpp
113 lines (108 loc) · 2.24 KB
/
14888.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 14888. 연산자 끼워넣기
// 2020.04.19
// 브루트 포스
#include<iostream>
#include<stack>
using namespace std;
int arr[101];
int op[4]; //+,-,*,/
int n;
int ansMax = -2100000000;
int ansMin = 2100000000;
stack<int> s;
// 계산한 값들에 대한 후처리
void calc(stack<int>& s, int a, int b)
{
s.pop();
s.push(b);
s.push(a);
}
void go(int cnt, int opCode, int value)
{
if (cnt == n - 1)
{
if (value > ansMax)
{
ansMax = value;
}
if (value < ansMin)
{
ansMin = value;
}
return;
}
for (int i = 0; i < 4; i++)
{
if (op[i] > 0)
{
int a = s.top();
s.pop();
int b = s.top();
s.pop();
// 더하기 일때
if (i == 0)
{
s.push(a + b);
op[i]--;
go(cnt + 1, i, a + b);
op[i]++;
calc(s, a, b);
}
// 빼기 일때
else if (i == 1)
{
s.push(a - b);
op[i]--;
go(cnt + 1, i, a - b);
op[i]++;
calc(s, a, b);
}
// 곱하기 일때
else if (i == 2)
{
s.push(a * b);
op[i]--;
go(cnt + 1, i, a * b);
op[i]++;
calc(s, a, b);
}
// 나누기 일때
else
{
// 음수 처리
if (a < 0)
{
s.push(((a * -1) / b) * -1);
}
else
{
s.push(a / b);
}
op[i]--;
go(cnt + 1, i, a / b);
op[i]++;
calc(s, a, b);
}
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 4; i++)
{
cin >> op[i];
}
// 순서대로 스택에 저장
for (int i = n - 1; i > -1; i--)
{
s.push(arr[i]);
}
go(0, 0, 0);
cout << ansMax << endl << ansMin << endl;
return 0;
}