자라는 개발자/문제풀이

백준 c++ 15815 천재 수학자 성필

자란다 2022. 2. 1. 22:54
728x90
반응형

15815 천재 수학자 성필


문제풀이

#include <iostream>
#include <stack>
using namespace std;
void fast_io(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int main()
{
    fast_io();
    string str;
    stack<int> s;
    cin >> str;
    int i = 0, res = 0;
    while (str[i] != '\0')
    {
        if (isdigit(str[i]))
            s.push(str[i] - 48);
        else
        {
            int b = s.top();
            s.pop();
            int a = s.top();
            s.pop();
            if (str[i] == '+')
                s.push(a + b);
            else if (str[i] == '*')
                s.push(a * b);
            else if (str[i] == '-')
                s.push(a - b);
            else if (str[i] == '/')
                s.push(a / b);
        }
        i++;
    }
    cout << s.top();
}

/ 나 - 할 때에 a와 b의 위치를 잘못 잡아줘서 계속 틀렸다..

728x90
반응형

'자라는 개발자 > 문제풀이' 카테고리의 다른 글

백준 c++ 1417 국회의원 선거  (0) 2022.02.04
백준 c++ 4158 CD  (0) 2022.02.03
백준 c++ 17413 단어 뒤집기 2  (0) 2022.01.31
백준 c++ 7568 덩치  (0) 2022.01.29
백준 c++ 2292 벌집  (0) 2022.01.29