zoukankan      html  css  js  c++  java
  • 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<<'
    ';
                        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()<<'
    ';
                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<<'
    ';
                //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<<'
    ';
                        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<<'
    ';
                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()<<'
    ';
                if(A[0]==24)puts("Yes");
                else puts("No");
                //st.pop();
            }
        }
    
    }
  • 相关阅读:
    van Emda Boas
    斐波那契堆
    NTT
    FFT
    KDTree
    扩展kmp
    kmp
    Dancing Links
    树的prufer编码
    有向图最小路径覆盖
  • 原文地址:https://www.cnblogs.com/liulex/p/11224249.html
Copyright © 2011-2022 走看看