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

sjmxxxx 2019-04-29 12:16:58
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;

}
...全文
218 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
good excellent perfect
sjmxxxx 2019-04-29
  • 打赏
  • 举报
回复
找到了,第14行和第40行的stack设置错了,应该是int

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

}
636f6c696e 2019-04-29
  • 打赏
  • 举报
回复
good!!

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧