题目:P1981
代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
int main()
{
//freopen("P1981_2.in","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string a;
cin>>a;
long long l=a.size(),t=0;
stack<long long> sz;
stack<char> fh;
for(long long i=0;i<l;i++)
{
if(a[i]>='0' && a[i]<='9')
{
t=(t*10+(a[i]-'0'))%10000;
}
if(a[i]=='+' || a[i]=='*')
{
sz.push(t%10000);
t=0;
if(fh.empty())
{
fh.push(a[i]);
}
else
{
if(a[i]=='*')
{
fh.push(a[i]);
}
else
{
if(fh.top()=='*')
{
long long b=sz.top();
sz.pop();
long long c=sz.top();
sz.pop();
sz.push((b*c)%10000);
fh.pop();
fh.push(a[i]);
}
else
{
fh.push(a[i]);
}
}
}
}
}
sz.push(t);
while(!fh.empty())
{
long long b=sz.top();
sz.pop();
long long c=sz.top();
sz.pop();
if(fh.top()=='+')
{
sz.push((b+c)%10000);
}
else
{
sz.push(b*c%10000);
}
fh.pop();
}
cout<<sz.top()%10000<<endl;
}
样例都过了,大佬们帮帮忙吧,谢谢。