CCF2019-3-2二十四点

#include <iostream>
#include <string>
#include <vector>

using namespace std;
const int N = 1000;

struct Count {
    string s;
    bool b;
}dis[100];

int main() {
    int n;
    int a;
    char b;
    int sum = 0;
    vector<int> p;
    vector<char> q;
    cin >> n;
    getchar();
    for (int i = 0; i < n; i++) {
        //输入字符串
        getline(cin, dis[i].s);
        for (int j = 0; j < 7; j += 2) {
            a = dis[i].s[j] - '0';
            p.push_back(a);
        }
        for (int j = 1; j < 6; j += 2) {
            b = dis[i].s[j];
            q.push_back(b);
        }
        //四则运算
        //计算乘除
        for (int j = q.size() - 1; j >= 0; j--) {
            if (q[j] == 'x') {
                int x = p[j];
                int y = p[j + 1];
                int z = x*y;
                p[j] = z;
                p.erase(p.begin() + j + 1);
                q.erase(q.begin() + j);
            }
            else if (q[j] == '/') {
                int x = p[j];
                int y = p[j + 1];
                int z = x/y;
                p[j] = z;
                p.erase(p.begin() + j + 1);
                q.erase(q.begin() + j);
            }
        }
        //计算加减法
        for (int j = q.size() - 1; j >= 0; j--) {
            if (q[j] == '+') {
                int x = p[j];
                int y = p[j + 1];
                int z = x + y;
                p[j] = z;
                p.erase(p.begin() + j + 1);
                q.erase(q.begin() + j);
            }
            else if (q[j] == '-') {
                int x = p[j];
                int y = p[j + 1];
                int z = x - y;
                p[j] = z;
                p.erase(p.begin() + j + 1);
                q.erase(q.begin() + j);
            }
        }
        sum = p[0];
        //设置标记位
        if (sum == 24)
            dis[i].b = true;
        else
            dis[i].b = false;
    }

    //输出
    for (int i = 0; i < n; i++) {
        if (dis[i].b == true)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}

推荐阅读更多精彩内容