2019/3/2 CCF测试第二题二十四点
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;
}
...全文
相关推荐
CCF/CSP2013-2019第一题C++,JAVA源码
这是一份CCF/CSP从2013-2019年第一题所有答案,答案全部是自己写的都已经运行到100分,里面包含CPP和JAVA的源码,有需要的大家自行下载参考吧
ccf历年试题百分题解(第二题合集)
这是到目前为止的ccf考试的历年测试题,压缩包内只有历年的第二题的答案(百分通过),没有其他答案,也没有题目!
20190302_ccf2019.3.2_
2019年3月的ccf测试中第二题的答案
ccf历年试题百分题解(第三题合集)
这是到目前为止的ccf考试的历年测试题,压缩包内只有历年的第三题的答案(百分通过),没有其他答案,也没有题目!
CCF考试历年真题代码(第一题)
CCF考试历年真题代码(第一题),大部分自己写的,亲测有效,基于JAVA编写


