-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path201903-2.cpp
85 lines (81 loc) · 1.77 KB
/
201903-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "stdio.h"
#include "string"
#include "iostream"
#include "stack"
// 需要考虑减法操作,如果使用栈的话最好将减法转为加法,此题也可以考虑队列来做
int main() {
int n;
std::string str;
std::stack<int> number;
std::stack<char> op;
char op_temp;
int a, b;
int c;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
std::cin >> str;
int sum = 0;
for (int j = 0; j < str.size(); ++j) {
if (str[j] > '0' && str[j] <= '9') {
number.push(str[j] - '0');
}
if (str[j] == 'x') {
j++;
a = str[j] - '0';
b = number.top();
number.pop();
c = b * a;
number.push(c);
}
if (str[j] == '/') {
j++;
a = str[j] - '0';
b = number.top();
number.pop();
c = b / a;
number.push(c);
}
if (str[j] == '+') {
op.push(str[j]);
}
if (str[j] == '-'){
j++;
number.push('0'-str[j]);
op.push('+');
}
}
while (!op.empty()) {
a = number.top();
number.pop();
b = number.top();
number.pop();
op.pop();
c = b + a;
number.push(c);
}
sum = number.top();
number.pop();
if (sum == 24) {
printf("Yes");
} else {
printf("No");
}
if (i < n - 1) {
printf("\n");
}
}
return 0;
}
/*
10
9+3+4x3
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4+4/5
*/