• 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
    */
  • 相关阅读:
    git本地分支管理
    SpringMVC的第一个入门案例
    原来热加载如此简单,手动写一个 Java 热加载吧
    Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
    SpringBoot 开发案例之参数传递的正确姿势
    MENU Java 中的 final、finally、finalize 有什么不同?
    Java Web 学习(1) —— Servlet
    Spring 基础知识学习
    SpringBoot中如何灵活的实现接口数据的加解密功能?
    Java中常用的四种线程池
  • 原文地址:https://www.cnblogs.com/-Asurada-/p/14387008.html
Copyright © 2020-2023  润新知