64,642
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
string exp;
cin >> exp;
stack<char> s1;
stack<char> s2;
for(int i = 0 ; i < exp.length(); i++){
if(exp[i] >= '1' && exp[i] <= '9'){
s1.push(exp[i] - '0');
}else if(exp[i] == '+' || exp[i] == '-'){
s2.push(exp[i]);
}else{
int n1,n2;
switch(exp[i]){
case 'x':
n1 = s1.top();
s1.pop();
n2 = n1 * (exp[++i] - '0');
s1.push(n2);
break;
case '/':
n1 = s1.top();
s1.pop();
n2 = n1 / (exp[++i] - '0');
s1.push(n2);
break;
}
}
}
stack<int> s3;
stack<int> s4;
while(!s1.empty()){
s3.push(s1.top());
s1.pop();
}
while(!s2.empty()){
s4.push(s2.top());
s2.pop();
}
while(!s4.empty()){
char c = s4.top();
//cout << "c:=" << c << " ";
s4.pop();
int temp1 = s3.top();
s3.pop();
int temp2 = s3.top();
//cout << "temp1=" << temp1 << " temp2=" << temp2 << endl;
s3.pop();
switch(c){
case '+':
//cout << "temp1 + temp2 = " << temp1 + temp2 << " " << endl;
s3.push(temp1 + temp2);
break;
case '-':
//cout << "temp1 - temp2 = " << temp1 - temp2 << " " << endl;;
s3.push(temp1 - temp2);
break;
}
//cout << "s1.size = " << s1.size() << endl;
}
int res = s3.top();
//cout<<" res= " << res << endl;
if( res == 24) cout << "Yes" << endl;
else cout << "No" << endl;
}
//system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
string exp;
cin >> exp;
stack<int> s1;
stack<char> s2;
for(int i = 0 ; i < exp.length(); i++){
if(exp[i] >= '1' && exp[i] <= '9'){
s1.push(exp[i] - '0');
}else if(exp[i] == '+' || exp[i] == '-'){
s2.push(exp[i]);
}else{
int n1,n2;
switch(exp[i]){
case 'x':
n1 = s1.top();
s1.pop();
n2 = n1 * (exp[++i] - '0');
s1.push(n2);
break;
case '/':
n1 = s1.top();
s1.pop();
n2 = n1 / (exp[++i] - '0');
s1.push(n2);
break;
}
}
}
stack<int> s3;
stack<char> s4;
while(!s1.empty()){
s3.push(s1.top());
s1.pop();
}
while(!s2.empty()){
s4.push(s2.top());
s2.pop();
}
while(!s4.empty()){
char c = s4.top();
//cout << "c:=" << c << " ";
s4.pop();
int temp1 = s3.top();
s3.pop();
int temp2 = s3.top();
//cout << "temp1=" << temp1 << " temp2=" << temp2 << endl;
s3.pop();
switch(c){
case '+':
//cout << "temp1 + temp2 = " << temp1 + temp2 << " " << endl;
s3.push(temp1 + temp2);
break;
case '-':
//cout << "temp1 - temp2 = " << temp1 - temp2 << " " << endl;;
s3.push(temp1 - temp2);
break;
}
//cout << "s1.size = " << s1.size() << endl;
}
int res = s3.top();
//cout<<" res= " << res << endl;
if( res == 24) cout << "Yes" << endl;
else cout << "No" << endl;
}
// system("pause");
return 0;
}