题目链接:http://118.190.20.162/view.page?gpid=T88
思路:后缀表达式求值
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <map>
using namespace std;
string str;
int n;
queue<char> Back;
stack<char> tmp;
stack<int> number;
map<char,int> priority;
bool res[100+10];
int main() {
cin>>n;
priority['+']=1;priority['-']=1;priority['x']=2;priority['/']=2;
for(int i=0;i<n;i++){
string str;
cin>>str;
int sum=0;
for(int j=0;j<str.size();j++){
if(str[j]>='0'&&str[j]<='9'){
Back.push(str[j]);
}
else
{
if(!tmp.empty()&&priority[str[j]]>priority[tmp.top()]){
tmp.push(str[j]);
}
else
{
while(!tmp.empty()&&priority[tmp.top()]>=priority[str[j]]){
char c=tmp.top();
Back.push(c);
tmp.pop();
}
tmp.push(str[j]);
}
}
}
while(!tmp.empty()){
char c=tmp.top();
Back.push(c);
tmp.pop();
}
while(!Back.empty()){
char c=Back.front();
Back.pop();
if(c>='0'&&c<='9'){
number.push(c-'0');
}
else
{
int a=number.top();
number.pop();
int b=number.top();
number.pop();
if(c=='+'){
sum=a+b;
number.push(sum);
}
else if(c=='-'){
sum=b-a;
number.push(sum);
}
else if(c=='x'){
sum=a*b;
number.push(sum);
}
else if(c=='/'){
sum=b/a;
number.push(sum);
}
}
}
if(number.top()==24)
res[i]=1;
else
res[i]=0;
}
for(int i=0;i<n;i++){
if(res[i])
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}