csp201903-2 二十四点

tech2022-12-30  123

csp 201903-2 二十四点

#include <iostream> #include <vector> #include <string> using namespace std; int calculate() { vector<int> s, v, t; string L; cin >> L; for (int i = 0; i < L.size(); i++) { if (L[i] >= '0' && L[i] <= '9') { v.push_back((int)(L[i] - '0')); } else { s.push_back(L[i]); } } t.push_back(v[0]); for (int i = 0; i < 3; i++) { if (s[i] == 'x') { int m = t.back(); t.pop_back(); t.push_back(m * v[i + 1]); } else if (s[i] == '/') { int m = t.back(); t.pop_back(); t.push_back(m / v[i + 1]); } else if (s[i] == '-'){ t.push_back(-v[i+1]); } else { t.push_back(v[i + 1]); } } int sum = 0; for (int i = 0; i < t.size(); i++) { sum += t[i]; } return sum; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { if (calculate() == 24) { cout << "Yes"; } else { cout << "No"; } if (i != n - 1) { cout << endl; } } return 0; }

最新回复(0)