#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <stack>
#include <map>
using namespace std;
stack<int> num;
stack<char> ops;
map<char, int> prio;
void compute() {
int num1 = num.top();
num.pop();
int num2 = num.top();
num.pop();
char op = ops.top();
ops.pop();
switch (op) {
case 'x':
num.push(num2 * num1);
break;
case '/':
num.push(num2 / num1);
break;
case '+':
num.push(num2 + num1);
break;
case '-':
num.push(num2 - num1);
break;
}
}
int main() {
prio['+'] = prio['-'] = 0;
prio['x'] = prio['/'] = 1;
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
num.push(s[0] - '0');
ops.push(s[1]);
num.push(s[2] - '0');
for (int j = 3; j < s.length(); j++) {
if (s[j] >= '0' && s[j] <= '9') {
num.push(s[j] - '0');
} else {
while (!ops.empty() && prio[s[j]] <= prio[ops.top()]) {
compute();
}
ops.push(s[j]);
}
}
while (!ops.empty())
compute();
printf(num.top() == 24 ? "Yes\n" : "No\n");
num.pop();
}
return 0;
}
参考1