解析:暴力枚举。建议用Python的eval函数,C++手写略麻烦。
#include <iostream>
#include <string>
#include <vector>
#include <sstream>using namespace std;long long stringResult(const string &expr) {vector<string> plusParts;stringstream ss(expr);string temp;while (getline(ss, temp, '+')) {plusParts.push_back(temp);}long long sum = 0;for (const auto &part : plusParts) {long long res = 1;stringstream ssPart(part);vector<string> multiplyParts;while (getline(ssPart, temp, '*')) {multiplyParts.push_back(temp);}for (const auto &mpart : multiplyParts) {res *= stoll(mpart);}sum += res;}return sum;
}bool equals(const string &s, long long another) {string sb = s;for (size_t i = 0; i < s.length(); i++) {char c = s[i];if (c >= '0' && c <= '9') {for (int j = 0; j < 10; j++) {sb.insert(i + 1, to_string(j));if (stringResult(sb) == another) {return true;}sb.erase(i + 1, 1);}}}return false;
}int main() {int n;cin >> n;vector<string> strs(n);for (int i = 0; i < n; i++) {string left, right;string equation;cin >> equation;size_t pos = equation.find('=');left = equation.substr(0, pos);right = equation.substr(pos + 1);long long leftRes = stringResult(left);long long rightRes = stringResult(right);if (leftRes == rightRes) {cout << "Yes" << endl;} else {if (equals(left, rightRes)) {cout << "Yes" << endl;} else if (equals(right, leftRes)) {cout << "Yes" << endl;} else {cout << "No" << endl;}}}}