CCF 201903-2 二十四点

#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;

int main()
{
    int n;
    cin>>n;
    stack <char> oper;//运算符
    stack <int> number;//操作数
    while(n--)
    {
        int end;
        string temp;
        cin>>temp;
        for(int i=0; i<(int)temp.size(); i++)
        {
            if(temp.at(i)>='0'&&temp.at(i)<='9')
                number.push(temp.at(i)-'0');
            else if(temp.at(i)=='x'||temp.at(i)=='/')
            {
                while(!oper.empty())
                {
                    char  o=oper.top();
                    int numberend;
                    if(o=='x'||o=='/')
                    {
                        int number1,number2;
                        number2=number.top();
                        number.pop();
                        number1=number.top();
                        number.pop();
                        switch (o)
                        {
                        case 'x':
                            numberend=number1*number2;
                            break;
                        case '/':
                            numberend=number1/number2;
                            break;
                        }
                        number.push(numberend);
                        oper.pop();//chuzhan
                    }
                    else
                        break;
                }
                oper.push(temp.at(i));//ruzhan
            }
            else
            {

                while(!oper.empty())
                {
                    char  o=oper.top();
                    int numberend;
                    int number1,number2;
                    number2=number.top();
                    number.pop();
                    number1=number.top();
                    number.pop();
                    switch (o)
                    {
                    case '+':
                        numberend=number1+number2;
                        break;
                    case '-':
                        numberend=number1-number2;
                        break;
                    case 'x':
                        numberend=number1*number2;
                        break;
                    case '/':
                        numberend=number1/number2;
                        break;

                    }
                    number.push(numberend);
                    oper.pop();
                }
                oper.push(temp.at(i));
            }
        }
        while(!oper.empty())
        {
            char  o=oper.top();
            int numberend;
            int number1,number2;
            number2=number.top();
            number.pop();
            number1=number.top();
            number.pop();
            switch (o)
            {
            case '+':
                numberend=number1+number2;
                break;
            case '-':
                numberend=number1-number2;
                break;
            case 'x':
                numberend=number1*number2;
                break;
            case '/':
                numberend=number1/number2;
                break;
            }
            number.push(numberend);
                oper.pop();
        }
        //number.push(numberend);

        //oper.push(temp.at(i));
        end=number.top();
        if(end==24)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
}
/*
10
9+3+4x3
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4+4/5
*/

 

posted @ 2021-02-07 22:40  -Asurada-  阅读(111)  评论(0编辑  收藏  举报