-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15658.cpp
More file actions
94 lines (86 loc) · 1.21 KB
/
15658.cpp
File metadata and controls
94 lines (86 loc) · 1.21 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 15658. 연산자 끼워넣기 (2)
// 2019.08.23
// 브루트 포스
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int n;
int num[11];
int op[4];
int arr[11];
int opCnt;
int minA = 2100000000;
int maxA = -2100000000;
// 계산
void calculate()
{
stack<int> s;
s.push(num[0]);
for (int i = 1; i < n; i++)
{
int first = s.top();
int second = num[i];
s.pop();
if (arr[i - 1] == 0)
{
s.push(first + second);
}
else if (arr[i - 1] == 1)
{
s.push(first - second);
}
else if (arr[i - 1] == 2)
{
s.push(first * second);
}
else if (arr[i - 1] == 3)
{
if (first < 0)
{
s.push(((first*-1) / second)*-1);
}
else
{
s.push(first / second);
}
}
}
minA = min(minA, s.top());
maxA = max(maxA, s.top());
}
void go(int cnt)
{
// 연산자를 모두 골랐다면 계산을함
if (cnt == n - 1)
{
calculate();
return;
}
for (int i = 0; i < 4; i++)
{
if (op[i] > 0)
{
op[i]--;
arr[cnt] = i;
go(cnt + 1);
op[i]++;
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
for (int i = 0; i < 4; i++)
{
cin >> op[i];
opCnt += op[i];
}
go(0);
cout << maxA << endl << minA << endl;
return 0;
}