-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[level 2] Title: [카카오 인턴] 수식 최대화, Time: 0.02 ms, Memory: 4.02 MB -Bae…
…kjoonHub
- Loading branch information
1 parent
e0edea4
commit c79b711
Showing
2 changed files
with
2 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
|
||
### 제출 일자 | ||
|
||
2023년 10월 2일 10:4:25 | ||
2023년 10월 2일 10:4:37 | ||
|
||
### 문제 설명 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,9 @@ | ||
#include <string> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
void strtok(vector<long long>& v_num, vector<char>& v_op, const char* s) | ||
{ | ||
string temp = ""; | ||
|
||
while (*s != '\0') | ||
{ | ||
if (*s == '*' || *s == '+' || *s == '-') | ||
{ | ||
v_op.emplace_back(*s); | ||
v_num.emplace_back(stoll(temp)); | ||
temp = ""; | ||
} | ||
else temp += *s; | ||
++s; | ||
} | ||
|
||
v_num.emplace_back(stoll(temp)); | ||
} | ||
|
||
long long calc(char op, long long num1, long long num2) | ||
{ | ||
switch (op) | ||
{ | ||
case '*': | ||
return num1 * num2; | ||
|
||
case '+': | ||
return num1 + num2; | ||
|
||
case '-': | ||
return num1 - num2; | ||
} | ||
} | ||
|
||
long long solution(string expression) | ||
{ | ||
long long solution(string expression) { | ||
long long answer = 0; | ||
vector<long long> s_num; | ||
vector<char> s_op; | ||
strtok(s_num, s_op, expression.c_str()); | ||
|
||
string op = "+-*"; | ||
sort(op.begin(), op.end()); | ||
|
||
do | ||
{ | ||
vector<long long> temp_num = s_num; | ||
vector<char> temp_op = s_op; | ||
|
||
for (size_t i = 0; i < op.size(); i++) | ||
{ | ||
char C = op[i]; | ||
|
||
for (size_t j = 0; j < temp_op.size(); j++) | ||
{ | ||
if (temp_op[j] == C) | ||
{ | ||
long long sum = calc(C, temp_num[j], temp_num[j + 1]); | ||
temp_num[j] = sum; | ||
|
||
// J번 Index는 결과값으로 바꿔버리고, J + 1번 Index는 삭제를 시켜버리는 것 | ||
temp_num.erase(temp_num.begin() + j + 1); | ||
temp_op.erase(temp_op.begin() + j); | ||
j--; // 모든 데이터의 Index값이 하나씩 땡겨지기 때문에 포인터를 한 칸 감소시켜 줘야 한다는 과정 | ||
} | ||
} | ||
} | ||
answer = max(answer, abs(temp_num[0])); | ||
} while (next_permutation(op.begin(), op.end())); | ||
|
||
return answer; | ||
} |