• 二十四点游戏


    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stack>
    using namespace std;
    
    
    bool lessthan(char a, char b)
    {
        if(b=='*' || b=='/' )
        {
            return true;
        }
    
        if(a=='+' || a=='-' )
        {
            return true;
        }
        return false;
    }
    
    void compute(stack<int> & s_num, char op)
    {
        int b = s_num.top();
        s_num.pop();
        int a = s_num.top();
        s_num.pop();
    
        if(op=='*')
        {
            s_num.push(a*b);
        }
        else if(op=='/')
        {
            s_num.push(a/b);
        }
        else if(op=='+')
        {
            s_num.push(a+b);
        }
        else if(op=='-')
        {
            s_num.push(a-b);
        }
        else
        {
        }
    }
    
    
    int expression(char *str, int num)
    {
        int res = 0;
        stack<int> s_num;
        stack<char> s_op;
        for(int i=0; i<num; i++)
        {
            if(str[i]>='0' && str[i]<='9')
            {
                int tmp = 0;
                int base = 1;
                tmp = base*(str[i]-'0');
                while(str[i+1]>='0' && str[i+1]<='9')
                {
                    tmp = tmp*10 + (str[i+1]-'0');
                    i++;
                }
                s_num.push(tmp);
            }
    
            else if(str[i] == '(')
            {
                s_op.push(str[i]);
            }
            else if(str[i] == '[')
            {
                s_op.push(str[i]);
            }
            else if(str[i] == '{')
            {
                s_op.push(str[i]);
            }
    
            else if(str[i] == ')')
            {
                char tmp = s_op.top();
                while(tmp!='(')
                {
                    s_op.pop();
                    //
                    compute(s_num, tmp);
    
                    tmp = s_op.top();
                }
                s_op.pop();
            }
            else if(str[i] == ']')
            {
                char tmp = s_op.top();
                while(tmp!='[')
                {
                    s_op.pop();
                    //
                    compute(s_num, tmp);
    
                    tmp = s_op.top();
                }
                s_op.pop();
            }
            else if(str[i] == '}')
            {
                char tmp = s_op.top();
                while(tmp!='{')
                {
                    s_op.pop();
                    //
                    compute(s_num, tmp);
    
                    tmp = s_op.top();
                }
                s_op.pop();
            }
    
            //*/+-
            else if( str[i]=='-' )
            {
                if( i==0 || str[i-1]=='(' ||str[i-1]=='{' ||str[i-1]=='[' ||str[i-1]=='+' ||str[i-1]=='-' ||str[i-1]=='*' ||str[i-1]=='/' )
                {
                    i++;
                    if(str[i]>='0' && str[i]<='9')
                    {
                        int tmp = 0;
                        int base = 1;
                        tmp = base*(str[i]-'0');
                        while(str[i+1]>='0' && str[i+1]<='9')
                        {
                            tmp = tmp*10 + (str[i+1]-'0');
                            i++;
                        }
                        s_num.push(0-tmp);
                    }
                    else
                    {
                        cout<<"error"<<endl;
                    }
                }
                else
                {
                    while(!s_op.empty())
                    {
                        char tmp = s_op.top();
                        if( tmp!='(' && tmp!='[' && tmp!='{' && lessthan(str[i], tmp))
                        {
                            s_op.pop();
                            //
                            compute(s_num, tmp);
                        }
                        else
                        {
                            break;
                        }
                    }
    
                    s_op.push(str[i]);
                }
            }
            else if(str[i]=='*' || str[i]=='/' || str[i]=='+')
            {
                while(!s_op.empty())
                {
                    char tmp = s_op.top();
                    if( tmp!='(' && tmp!='[' && tmp!='{' && lessthan(str[i], tmp))
                    {
                        s_op.pop();
                        //
                        compute(s_num, tmp);
    
                    }
                    else
                    {
                        break;
                    }
                }
    
                s_op.push(str[i]);
            }
            else
            {
            }
    
        }
    
        //final
        while(!s_op.empty())
        {
            char tmp = s_op.top();
            compute(s_num, tmp);
            s_op.pop();
        }
    
        return s_num.top();
    }
    
    
    
    void solve(int count, string *four, string *plusx, bool &flag)
    {
        string fuhao[4] = {"+","-", "*", "/"};
        if(flag)
        {
            return;
        }
        else
        {
            if(count == 3)
            {
                string total;
                if(plusx[0]=="/" && plusx[1]=="*" && plusx[2]=="*")// /**
                {
                    total = four[0]  + plusx[1] + four[2] + plusx[2] + four[3]+ plusx[0] + four[1];
                }
                else if(plusx[0]=="/" && plusx[1]=="*")
                {
                    total = four[0]  + plusx[1] + four[2] + plusx[0] + four[1]+ plusx[2] + four[3];
                }
                else if(plusx[1]=="/" && plusx[2]=="*")
                {
                    total = four[0]  + plusx[0] + four[1]+ plusx[2] + four[3]+ plusx[1] + four[2] ;
                }
                else
                {
                    total = four[0]  + plusx[1] + four[1]+ plusx[2]+ four[2] + plusx[0]  + four[3];
                }
    
                char expr[100];
    
                int len = total.length();
    
                for(int i=0; i<len; i++)
                {
                    expr[i] = total[i];
                }
                expr[len] = '';
    
                //cout<<expr<<"=";
    
                float tmp = expression(expr, len);
                //cout<<tmp<<endl;
    
                if(tmp - 24 < 0.001 && tmp - 24>-0.001)
                {
                    flag = true;
                }
                else
                {
                }
    
            }
            else
            {
                for(int i=0; i<4; i++)
                {
                    if(flag==false)
                    {
                        plusx[count] = fuhao[i];
                        solve(count+1, four, plusx, flag);
                    }
                }
            }
        }
        if(count==0 && flag==false)
        {
            cout<<"NONE";
        }
    }
    
    string trans(string str)
    {
        if(str=="J")
        {
            str="11";
            return str;
        }
        else if(str=="Q")
        {
            str="12";
            return str;
        }
        else if(str=="K")
        {
            str="13";
            return str;
        }
        else if(str=="A")
        {
            str="1";
            return str;
        }
        else
        {
            return str;
        }
    }
    
    int main()
    {
    /*
        char str[100];
        cin.get(str, 100);
        int N = strlen(str);
    
        cout<<expression(str, N)<<endl;
        cout<<"true"<<endl;
    */
        string four[4];
        string tran[4];
        for(int i=0; i<4; i++)
        {
            cin>>four[i];
        }
    
        for(int i=0; i<4; i++)
        {
            if(four[i]=="joker" || four[i]=="JOKER")
            {
                cout<<"ERROR";
                return 0;
            }
            else
            {
                tran[i] = trans(four[i]);
            }
        }
    
        string plusx[3];
        bool flag = false;
        solve(0, tran, plusx, flag);
    
        if(flag)
            cout<<four[0]  + plusx[1] + four[1]+ plusx[2]+ four[2] + plusx[0]  + four[3];
    
        return 0;
    }
    

      

  • 相关阅读:
    设计模式-1.12备忘录模式
    设计模式-简介
    设计模式-1.9享元模式
    设计模式-1.8组合模式
    设计模式-1.7外观模式
    设计模式-1.6建造者模式(生成器模式)
    设计模式-1.5原型模式
    我在GitHubPage的博客
    奇怪的友链增加啦!
    SSL-OI夏日合宿 杂题 LOJ#6089小Y的背包计数问题 根号分治
  • 原文地址:https://www.cnblogs.com/hardsoftware/p/6231041.html
Copyright © 2020-2023  润新知