201903-2 CCF 二十四点

题面:

考场写的30分==

#include<bits/stdc++.h>
using namespace std;
stack<int>st;
stack<char>op;
int main()
{
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        string s;
        while(t--)
        {
            cin>>s;


            //cout<<s.length();
            for(int i=0; i<7; i++)
            {
                //cout<<i<<endl;
                if(s[i]>='0'&&s[i]<='9')
                    st.push(s[i]-'0');
                else if(s[i]=='/')
                {
                    int x=st.top();
                    //cout<<x<<'\n';
                    int y=s[i+1]-'0';
                    st.pop();
                    st.push(x/y);
                    i++;
                }
                else if(s[i]=='x')
                {
                    int x=st.top();
                    int y=s[i+1]-'0';
                    st.pop();
                    st.push(x*y);
                    //cout<<x<<y<<endl;
                    i++;
                }
                else
                {
                    op.push(s[i]);
                }


            }
            while(!op.empty())
            {
                char c=op.top();
                op.pop();
                int x=st.top();
                st.pop();
                int y=st.top();
                st.pop();
                if(c=='+')
                {
                    st.push(x+y);
                }
                else
                {
                    st.push(y-x);
                }
            }
            //cout<<st.top()<<'\n';
            if(st.top()==24)puts("Yes");
            else puts("No");
            st.pop();
        }
    }

}

hack数据:3x8-3+3

处理加减法没按照从前向后规则,然后炸了QAQ

修改后满分代码:

#include<bits/stdc++.h>
using namespace std;
stack<int>st;
stack<char>op;
//stack<int>t;
int A[4];
char B[4];
int main()
{
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        string s;
        while(t--)
        {
            cin>>s;

           // cout<<s<<'\n';
            //cout<<s.length();
            for(int i=0; i<7; i++)
            {
                //cout<<i<<endl;
                if(s[i]>='0'&&s[i]<='9'){

                    st.push(s[i]-'0');
                }
                else if(s[i]=='/')
                {
                    int x=st.top();
                    //cout<<x<<'\n';
                    int y=s[i+1]-'0';
                    st.pop();
                    st.push(x/y);
                    i++;
                }
                else if(s[i]=='x')
                {
                    int x=st.top();
                    int y=s[i+1]-'0';
                    st.pop();
                    st.push(x*y);
                    //cout<<x<<y<<endl;
                    i++;
                }
                else
                {
                    op.push(s[i]);
                }


            }

            int a=0,b=0;
            while(!st.empty())
            {
                A[a++]=st.top();
                st.pop();
            }
            while(!op.empty())
            {
                B[b++]=op.top();
                op.pop();
            }
            //cout<<a<<' '<<b<<'\n';
            a--;
            b--;
            int x,y;
            while(b>=0)
            {
                x=A[a];
                y=A[a-1];
                if(B[b]=='+')
                {
                    A[a-1]=x+y;
                }
                else
                {
                    A[a-1]=x-y;
                }
                a--;
                b--;
            }
            //cout<<st.top()<<'\n';
            if(A[0]==24)puts("Yes");
            else puts("No");
            //st.pop();
        }
    }

}

 

posted @ 2019-07-22 10:12  liulex  阅读(542)  评论(0编辑  收藏  举报