ZJ d016: 後序運算法
Stack跑下去就對了
要注意的是數字可能不止一位
以下為code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define AC ios::sync_with_stdio(0),cin.tie(0);
int main()
{
AC
string str;
ll n1,n2;
while(getline(cin,str))
{
stack<ll,list<ll>> s;
string t;
istringstream iss(str);
while(iss>>t)
{
if(t=="+") n2=s.top(),s.pop(),n1=s.top(),s.pop(),s.push(n1+n2);
else if(t=="-") n2=s.top(),s.pop(),n1=s.top(),s.pop(),s.push(n1-n2);
else if(t=="*") n2=s.top(),s.pop(),n1=s.top(),s.pop(),s.push(n1*n2);
else if(t=="/") n2=s.top(),s.pop(),n1=s.top(),s.pop(),s.push(n1/n2);
else if(t=="%") n2=s.top(),s.pop(),n1=s.top(),s.pop(),s.push(n1%n2);
else s.push(stoll(t));
}
cout<<s.top()<<'\n';
}
}
留言
張貼留言