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;

}
...全文
267 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!!
资源来源于网络,如有侵权,私信立删。 CSP-J/S第一轮时间 CSP-J/S第一轮分为:CSP-J1和CSP-S1。和NOIP第一轮时间一样,CSP-J/S第一轮认证在10月份第三个星期六进行。 CSP-J/S第一轮考察重点 第一轮认证为笔试或机试,主要测试选手有关计算机方面的基本知识,第一轮为资格测试。从2020年开始,全部为机试。 CSP-J/S第一轮报名方式 参加CSP-S/J两组两轮认证均须在网上注册报名,注册网站为http://rg.noi.cn。参加认证者必须如实填写个人信息报名,包括但不限于姓名、身份证号、出生日期、性别、就学(学籍学校)/就职单位等,信息一旦注册,不得修改,如有错误,责任自负。 CSP-J/S第一轮认证考点 省认证点由CCF授权的CSP非专业级别省认证组织单位设置。 1、第一轮认证点:由省认证组织单位总负责人设置,每个认证点人数不应少于20人。 2、未经批准的认证点,其认证成绩不予承认。 认证费用 第一轮CSP-S/J组:50元/人(该费用不包括食宿及交通费)。 CSP-J/S第一轮认证试组成 CSP-J/S第一轮在10月份第三个星期六进行,其中CSP-J1认证时间为周六9:30-11:30,CSP-S1认证时间为周六14:30-16:30。考试时长为2小时,内容为笔试或机试(从2020年开始全部为机试),满分100分。 试由三部分组成: ① 选择(共15,每2分,共计30分) CSP-S1的前10道为单选,后10道为不定项选择(只有全部选对才得分,否则不得分);CSP-J1的前15道都是单选。 ② 程序阅读理解(共3,共计40分) 目给出一段程序(不一定有关于程序功能的说明),考生通过阅读理解该段程序进行答,分为选择和判断。 ③ 程序完善(共3,共计30分) 目给出一段关于程序功能的文字说明,然后给出一段程序代码,在代码中略去了若干个语句或语句的一部分并在这些位置给出空格,要求考生根据程序的功能说明和代码的上下文,选择对应答案 。三皆为选择

65,209

社区成员

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

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