2019/3/2 CCF测试第二题二十四点

sjmxxxx sjmxxxx 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
2019/3/2 CCF测试第二题二十四点 只获得70分想不通哪里错了,求大神解答

#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;

}
...全文
评论 3
收藏 已收藏
分享
返回顶部
sjmxxxx
C++ 语言 65,214 成员 250,514 内容