← Back to List

10331번: Miscalculation ↗

Solutions

C++14
937 B | 937 chars
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#define ll long long
using namespace std;

string a;
int N,A,B;
stack <int> stk;
int main()
{
    cin>>a>>N;
    B=a[0]-48;
    for(int x=1; x<a.length(); x+=2)
    {
        if(a[x]=='+')
        {
            B+=a[x+1]-48;
        }
        if(a[x]=='*')
        {
            B*=a[x+1]-48;
        }
    }
    for(int x=0; x<a.length(); x++)
    {
        if(x%2==0) stk.push(a[x]-48);
        if(x%2==1) 
        {
            if(a[x]=='*')
            {
                int k= stk.top();
                stk.pop();
                stk.push(k*(a[x+1]-48));
                x++;
            }
        }
    }
    while(!stk.empty())
    {
        A+=stk.top();
        stk.pop();
    }
    
    if(N==A&&N==B)
    {
        cout<<"U";
    }
    else if(N==A)
    {
        cout<<"M";
    }
    else if(N==B)
    {
        cout<<"L";
    }
    else cout<<"I";

}