模拟题,c[i]储存原串,用一个栈装数字,遇到乘号将栈顶元素取出与乘号后一个数字相乘,将乘积放入栈中。
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<string>
using namespace std;
const int N=105;
int main()
{
char c[N];
char c1[N];
int k;
stack<int> s;
while(~scanf("%s",c))
{
bool kk1=0,kk2=0;
cin>>k;
int len=strlen(c);
for(int i=0;i<len;i++)
{
if(i%2==0)
s.push(c[i]-'0');
else
{
if(c[i]=='*')
{
i++;
int temp=s.top();
temp*=c[i]-'0';
s.pop();
s.push(temp);
}
}
}
int ans=0;
while(!s.empty())
{
ans+=s.top();
s.pop();
}
if(ans==k)
kk1=1;
ans=c[0]-'0';
for(int i=1;i<len;i+=2)
{
if(c[i]=='*')
ans*=c[i+1]-'0';
else
ans+=c[i+1]-'0';
}
if(ans==k)
kk2=1;
if(kk1&&kk2)
cout<<"U"<<endl;
else if(!kk1&&kk2)
cout<<"L"<<endl;
else if(!kk1&&!kk2)
cout<<"I"<<endl;
else
cout<<"M"<<endl;
}
return 0;
}