CCF CSP 竞赛试题——二十四点(201903-2)

  • 碰到加减号,往前看;碰到乘除号,往后看。
#include <iostream>
#include <vector>
#include <cctype>
using namespace std;

int main() {
    int n;
    cin >> n;
    while (n--) {
        string s;
        cin >> s;
        char op;
        vector<int> v;
        for (int i = 0; i < 7; ++i) {
            if (isdigit(s[i])) {
                v.push_back(s[i] - '0');
            } else {
                if (s[i] == '+' || s[i] == '-') {
                    if (v.size() == 2) {
                        v[0] = op == '+' ? v[0] + v[1] : v[0] - v[1];
                        v.pop_back();
                    }
                    op = s[i];
                } else {
                    int x = s[i + 1] - '0';
                    v.back() = s[i] == 'x' ? v.back() * x : v.back() / x;
                    ++i;
                }
            }
        }
        if (v.size() == 2) v[0] = op == '+' ? v[0] + v[1] : v[0] - v[1];
        cout << (v[0] == 24 ? "Yes" : "No") << endl;
    }
    return 0;
}
发布了33 篇原创文章 · 获赞 4 · 访问量 8740

猜你喜欢

转载自blog.csdn.net/Touchig/article/details/102833221