1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include <iostream> #include <cstdio> #include <vector> #include <string> #include <deque> #include <algorithm> using namespace std; int calculate(int n1, int n2, char op) { switch (op) { case '+': return n1 + n2; break; case '-': return n1 - n2; break; case 'x': return n1 * n2; break; case '/': return n1 / n2; break; } return -1; } bool solve(string s) { deque<int> si; deque<char> sop; for (int i = 0; i < 7; i++) { if (i % 2 == 0) { // Inttop int t = s[i] - '0'; si.push_back(t); if (sop.empty()) continue; if (sop.back() == 'x' || sop.back() == '/') { int t2 = si.back(); si.pop_back(); int t1 = si.back(); si.pop_back(); int t = calculate(t1, t2, sop.back()); sop.pop_back(); si.push_back(t); } } else { // Op char op = s[i]; sop.push_back(op); } } while (!sop.empty()) { int t1 = si.front(); si.pop_front(); int t2 = si.front(); si.pop_front(); int t = calculate(t1, t2, sop.front()); sop.pop_front(); si.push_front(t); } return si.front()==24; } int main() { ios::sync_with_stdio(false); int n; string s; cin >> n; while (n--) { cin >> s; if (solve(s)) cout << "Yes\n"; else cout << "No\n"; } } |
CSP 201903-2 二十四点
发表评论