https://school.programmers.co.kr/learn/courses/30/lessons/67257
λ¬Έμ μ€λͺ
IT λ²€μ² νμ¬λ₯Ό μ΄μνκ³ μλ λΌμ΄μΈμ 맀λ μ¬λ΄ ν΄μ»€ν€ λνλ₯Ό κ°μ΅νμ¬ μ°μΉμμκ² μκΈμ μ§κΈνκ³ μμ΅λλ€.
μ΄λ² λνμμλ μ°μΉμμκ² μ§κΈλλ μκΈμ μ΄μ λνμλ λ€λ₯΄κ² λ€μκ³Ό κ°μ λ°©μμΌλ‘ κ²°μ νλ €κ³ ν©λλ€.
ν΄μ»€ν€ λνμ μ°Έκ°νλ λͺ¨λ μ°Έκ°μλ€μκ²λ μ«μλ€κ³Ό 3κ°μ§μ μ°μ°λ¬Έμ(+, -, *) λ§μΌλ‘ μ΄λ£¨μ΄μ§ μ°μ° μμμ΄ μ λ¬λλ©°, μ°Έκ°μμ λ―Έμ μ μ λ¬λ°μ μμμ ν¬ν¨λ μ°μ°μμ μ°μ μμλ₯Ό μμ λ‘κ² μ¬μ μνμ¬ λ§λ€ μ μλ κ°μ₯ ν° μ«μλ₯Ό μ μΆνλ κ²μ λλ€.
λ¨, μ°μ°μμ μ°μ μμλ₯Ό μλ‘ μ μν λ, κ°μ μμμ μ°μ°μλ μμ΄μΌ ν©λλ€. μ¦, + > - > * λλ - > * > + λ±κ³Ό κ°μ΄ μ°μ°μ μ°μ μμλ₯Ό μ μν μ μμΌλ +,* > - λλ * > +,-μ²λΌ 2κ° μ΄μμ μ°μ°μκ° λμΌν μμλ₯Ό κ°μ§λλ‘ μ°μ°μ μ°μ μμλ₯Ό μ μν μλ μμ΅λλ€. μμμ ν¬ν¨λ μ°μ°μκ° 2κ°λΌλ©΄ μ μν μ μλ μ°μ°μ μ°μ μμ μ‘°ν©μ 2! = 2κ°μ§μ΄λ©°, μ°μ°μκ° 3κ°λΌλ©΄ 3! = 6κ°μ§ μ‘°ν©μ΄ κ°λ₯ν©λλ€.
λ§μ½ κ³μ°λ κ²°κ³Όκ° μμλΌλ©΄ ν΄λΉ μ«μμ μ λκ°μΌλ‘ λ³ννμ¬ μ μΆνλ©° μ μΆν μ«μκ° κ°μ₯ ν° μ°Έκ°μλ₯Ό μ°μΉμλ‘ μ μ νλ©°, μ°μΉμκ° μ μΆν μ«μλ₯Ό μ°μΉμκΈμΌλ‘ μ§κΈνκ² λ©λλ€.
μλ₯Ό λ€μ΄, μ°Έκ°μ μ€ λ€μ€κ° μλμ κ°μ μμμ μ λ¬λ°μλ€κ³ κ°μ ν©λλ€.
"100-200*300-500+20"
μΌλ°μ μΌλ‘ μν λ° μ μ°νμμ μ½μλ μ°μ°μ μ°μ μμμ λ°λ₯΄λ©΄ λνκΈ°μ λΉΌκΈ°λ μλ‘ λλ±νλ©° κ³±νκΈ°λ λνκΈ°, λΉΌκΈ°μ λΉν΄ μ°μ μμκ° λμ * > +,- λ‘ μ°μ μμκ° μ μλμ΄ μμ΅λλ€.
λν κ·μΉμ λ°λΌ + > - > * λλ - > * > + λ±κ³Ό κ°μ΄ μ°μ°μ μ°μ μμλ₯Ό μ μν μ μμΌλ +,* > - λλ * > +,- μ²λΌ 2κ° μ΄μμ μ°μ°μκ° λμΌν μμλ₯Ό κ°μ§λλ‘ μ°μ°μ μ°μ μμλ₯Ό μ μν μλ μμ΅λλ€.
μμμ μ°μ°μκ° 3κ° μ£Όμ΄μ‘μΌλ―λ‘ κ°λ₯ν μ°μ°μ μ°μ μμ μ‘°ν©μ 3! = 6κ°μ§μ΄λ©°, κ·Έ μ€ + > - > * λ‘ μ°μ°μ μ°μ μμλ₯Ό μ νλ€λ©΄ κ²°κ΄κ°μ 22,000μμ΄ λ©λλ€.
λ°λ©΄μ * > + > - λ‘ μ°μ°μ μ°μ μμλ₯Ό μ νλ€λ©΄ μμμ κ²°κ΄κ°μ -60,420 μ΄μ§λ§, κ·μΉμ λ°λΌ μ°μΉ μ μκΈμ μ λκ°μΈ 60,420μμ΄ λ©λλ€.
μ°Έκ°μμκ² μ£Όμ΄μ§ μ°μ° μμμ΄ λ΄κΈ΄ λ¬Έμμ΄ expressionμ΄ λ§€κ°λ³μλ‘ μ£Όμ΄μ§ λ, μ°μΉ μ λ°μ μ μλ κ°μ₯ ν° μκΈ κΈμ‘μ return νλλ‘ solution ν¨μλ₯Ό μμ±ν΄μ£ΌμΈμ.
λ΄ νμ΄
vectorλ‘ κ΅¬ννλ€κ° vectorμ eraseμ μκ° λ³΅μ‘λκ° μ κ²½μ°μλ€. κ·Έλμ listλ‘ λ€μ ꡬννλλ° λλ€ μ‘μΈμ€κ° μλλ λΆνΈν¨ λλ¬Έμ κ²°κ΅ vectorλ‘ λ€μ λμμλ€. λ€νν vectorλ‘ κ΅¬νν΄λ μμ κΈΈμ΄κ° κΈΈμ§ μμμμΈμ§ μκ° μ΄κ³Όκ° λμ§ μμλ€.
#include <string>
#include <vector>
using namespace std;
long long calculate(long long _num1, long long _num2, char _operator)
{
if (_operator == '+')
return _num1 + _num2;
else if (_operator == '-')
return _num1 - _num2;
else if (_operator == '*')
return _num1 * _num2;
return -1;
}
long long solution(string expression) {
long long answer = 0;
vector<char> operators;
vector<string> operands;
vector<string> priority = { "+-*", "+*-", "-+*", "-*+", "*+-", "*-+" };
string cur;
for (const auto& e : expression)
{
if (e >= '0' && e <= '9')
{
cur += e;
}
else
{
operands.emplace_back(cur);
cur.clear();
operators.emplace_back(e);
}
}
operands.emplace_back(cur);
for (const auto& p : priority)
{
vector<char> operators_cpy = operators;
vector<string> operands_cpy = operands;
for (int i = 0; i < p.size(); ++i)
{
char cur_operator = p[i];
for (int j = 0; j < operators_cpy.size(); ++j)
{
if (operators_cpy[j] == cur_operator)
{
operands_cpy[j] = to_string(calculate(stoll(operands_cpy[j]), stoll(operands_cpy[j + 1]), cur_operator));
operators_cpy.erase(operators_cpy.begin() + j);
operands_cpy.erase(operands_cpy.begin() + j + 1);
--j;
}
}
}
long long result = abs(stoll(operands_cpy[0]));
answer = answer < result ? result : answer;
}
return answer;
}
'π₯οΈ Study Note > Coding Test' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[νλ‘κ·Έλλ¨Έμ€]level.2 - ν μ΄λΈ ν΄μ ν¨μ(C++) (0) | 2023.07.27 |
---|---|
[νλ‘κ·Έλλ¨Έμ€]level.3 - κ±°μ€λ¦λ(C++) (0) | 2023.07.25 |
[νλ‘κ·Έλλ¨Έμ€]level.2 - μ°μ λΆλΆ μμ΄ ν©μ κ°μ(C++) (0) | 2023.07.23 |
[νλ‘κ·Έλλ¨Έμ€]level.3 -νλ Έμ΄μ ν(C++) (2) | 2023.07.22 |
[νλ‘κ·Έλλ¨Έμ€]level.2 - μΊμ(C++) (0) | 2023.07.20 |