-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20190302.cpp
82 lines (82 loc) · 2.05 KB
/
20190302.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
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
{
string s;
cin >> s;
stack<int> num, st;
int len = s.length(), val;
for (int j = 0; j < len; ++j)
{
if (s[j] >= '0' && s[j] <= '9')
{
num.push(s[j] - '0');
}
else
{
switch (s[j])
{
case '+':
case '-':
if(!st.empty())
{
int flag = st.top(),v2,v1;
v2 = num.top();
num.pop();
v1 = num.top();
num.pop();
if (flag == 1)
val=v1+v2;
else if (flag == 2)
val=v1-v2;
num.push(val);
st.pop();
}
if(s[j]=='+')
st.push(1);
else
st.push(2);
break;
case 'x':
case '/':
val = num.top();
num.pop();
if(s[j]=='x')
val *= (s[j+1] - '0');
else
val /= (s[j+1] - '0');
++j;
num.push(val);
break;
}
}
}
while(!st.empty()){
int j = st.top(),v1,v2;
st.pop();
v2 = num.top();
num.pop();
v1 = num.top();
num.pop();
if(j==1){
num.push(v1 + v2);
}
else if(j==2){
num.push(v1 - v2);
}
}
if (num.top() == 24)
{
printf("Yes\n");
}
else
printf("No\n");
}
return 0;
}