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;
}
转载请注明原文地址:https://tech.qufami.com/read-8595.html