Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3fe9133

Browse files
committed
Lv2_수식최대화
1 parent 285198b commit 3fe9133

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

‎Programmers/Lv2/Lv2_수식최대화.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <string>
2+
#include <vector>
3+
#include <iostream>
4+
#include <algorithm>
5+
using namespace std;
6+
7+
long long cal(long long a, long long b, char op) {
8+
if (op == '+')
9+
return a + b;
10+
else if (op == '-')
11+
return a - b;
12+
else if (op == '*')
13+
return a * b;
14+
}
15+
16+
long long solution(string expression) {
17+
long long answer = 0;
18+
19+
vector<long long> nums;
20+
vector<char> oper;
21+
22+
string temp = "";
23+
for (int i = 0; i < expression.length(); ++i) {
24+
if (isdigit(expression[i]))
25+
temp += expression[i];
26+
else {
27+
oper.push_back(expression[i]);
28+
nums.push_back(stoll(temp));
29+
temp = "";
30+
}
31+
}
32+
nums.push_back(stoll(temp));
33+
34+
vector<int> order = { 0, 1, 2 };
35+
vector<char> ops = {'+', '-', '*'};
36+
do {
37+
vector<long long> temp_nums = nums;
38+
vector<char> temp_oper = oper;
39+
40+
for (int i = 0; i < order.size(); i++) {
41+
for (int j = 0; j < temp_oper.size(); ) {
42+
if (temp_oper[j] == ops[order[i]]) { // 내 순서에 맞춰서
43+
long long ans = cal(temp_nums[j], temp_nums[j + 1], temp_oper[j]);
44+
45+
temp_nums.erase(temp_nums.begin() + j);
46+
temp_nums.erase(temp_nums.begin() + j);
47+
temp_nums.insert(temp_nums.begin() + j, ans); // 2개 지우고 결과값 넣기
48+
49+
temp_oper.erase(temp_oper.begin() + j); // 연산자 한개 지우기
50+
}
51+
else
52+
j++;
53+
}
54+
}
55+
56+
answer = max(answer, abs(temp_nums[0]));
57+
} while (next_permutation(order.begin(), order.end()));
58+
59+
return answer;
60+
}
61+
62+
int main() {
63+
cout << solution("100-200*300-500+20");
64+
return 0;
65+
}

‎Programmers/Programmers.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@
241241
<ClCompile Include="Lv2\Lv2_쇠막대기.cpp">
242242
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
243243
</ClCompile>
244+
<ClCompile Include="Lv2\Lv2_수식최대화.cpp">
245+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
246+
</ClCompile>
244247
<ClCompile Include="Lv2\Lv2_숫자야구.cpp">
245248
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
246249
</ClCompile>

‎Programmers/Programmers.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,5 +534,8 @@
534534
<ClCompile Include="Lv2\Lv2_거리두기확인하기.cpp">
535535
<Filter>소스 파일</Filter>
536536
</ClCompile>
537+
<ClCompile Include="Lv2\Lv2_수식최대화.cpp">
538+
<Filter>소스 파일</Filter>
539+
</ClCompile>
537540
</ItemGroup>
538541
</Project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /