-
Notifications
You must be signed in to change notification settings - Fork 4
/
Lv2_수식최대화.cpp
65 lines (54 loc) · 1.73 KB
/
Lv2_수식최대화.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
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
long long cal(long long a, long long b, char op) {
if (op == '+')
return a + b;
else if (op == '-')
return a - b;
else if (op == '*')
return a * b;
}
long long solution(string expression) {
long long answer = 0;
vector<long long> nums;
vector<char> oper;
string temp = "";
for (int i = 0; i < expression.length(); ++i) {
if (isdigit(expression[i]))
temp += expression[i];
else {
oper.push_back(expression[i]);
nums.push_back(stoll(temp));
temp = "";
}
}
nums.push_back(stoll(temp));
vector<int> order = { 0, 1, 2 };
vector<char> ops = {'+', '-', '*'};
do {
vector<long long> temp_nums = nums;
vector<char> temp_oper = oper;
for (int i = 0; i < order.size(); i++) {
for (int j = 0; j < temp_oper.size(); ) {
if (temp_oper[j] == ops[order[i]]) { // 내 순서에 맞춰서
long long ans = cal(temp_nums[j], temp_nums[j + 1], temp_oper[j]);
temp_nums.erase(temp_nums.begin() + j);
temp_nums.erase(temp_nums.begin() + j);
temp_nums.insert(temp_nums.begin() + j, ans); // 2개 지우고 결과값 넣기
temp_oper.erase(temp_oper.begin() + j); // 연산자 한개 지우기
}
else
j++;
}
}
answer = max(answer, abs(temp_nums[0]));
} while (next_permutation(order.begin(), order.end()));
return answer;
}
int main() {
cout << solution("100-200*300-500+20");
return 0;
}