stack을 이용해서 문제를 풀 수 있다.
1. 연산자 (+, -, *, /)가 나오면 스택에서 숫자 두개를 꺼낸 다음에 (pop 2번) 연산을 하고 다시 스택에 push 한다.
2. 연산자가 아닐 경우에 스택에 push를 한다.
3. 반복문이 끝나면 스택의 top을 출력한다.
#include <bits/stdc++.h>
using namespace std;
int arr[26];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
stack<double> S;
string str;
cin >> str;
for (int i = 0; i < n; ++i) cin >> arr[i];
for (int i = 0; i < str.length(); ++i)
{
if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
double a = S.top(); S.pop();
double b = S.top(); S.pop();
if (str[i] == '+')
S.push(b + a);
else if (str[i] == '-')
S.push(b - a);
else if (str[i] == '*')
S.push(b * a);
else if (str[i] == '/')
S.push(b / a);
}
else
S.push(arr[str[i] - 'A']);
}
printf("%.2f", S.top());
}