zoukankan      html  css  js  c++  java
  • CCF-CSP题解 201903-2 二十四点

    可枚举。

    写栈的主要思想是:一个数栈(numSta),一个运算符栈(opSta)。遇到一个运算符,就把之前优先级(equal or greater than)它的运算符处理掉。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int operation(int num1, char op, int num2)
    {
        if (op == '+')
            return num1 + num2;
        else if (op == '-')
            return num1 - num2;
        else if (op == 'x')
            return num1 * num2;
        else
            return num1 / num2;
    }
    
    int getAns(char s[]) {
        stack<int> numSta;
        stack<char> opSta;
        for (int i = 1; i <= 7; i++)
        {
            if (s[i] >= '1' && s[i] <= '9')
                numSta.push(s[i] - '0');
            else
            {
                if (s[i] == '+' || s[i] == '-')
                {
                    while (!opSta.empty())
                    {
                        char op = opSta.top(); opSta.pop();
                        int num2 = numSta.top(); numSta.pop();
                        int num1 = numSta.top(); numSta.pop();
                        numSta.push(operation(num1, op, num2));
                    }
                    opSta.push(s[i]);
                }
                else
                {
                    while (!opSta.empty() && (opSta.top() == 'x' || opSta.top() == '/'))
                    {
                        char op = opSta.top(); opSta.pop();
                        int num2 = numSta.top(); numSta.pop();
                        int num1 = numSta.top(); numSta.pop();
                        numSta.push(operation(num1, op, num2));
                    }
                    opSta.push(s[i]);
                }
            }
        }
        while (!opSta.empty())
        {
            char op = opSta.top(); opSta.pop();
            int num2 = numSta.top(); numSta.pop();
            int num1 = numSta.top(); numSta.pop();
            numSta.push(operation(num1, op, num2));
        }
        return numSta.top();
    }
    
    int main ()
    {
        int n;
        scanf("%d", &n);
    
        while (n--)
        {
            char s[10];
            scanf("%s", s + 1);
            if (getAns(s) == 24)
                printf("Yes
    ");
            else
                printf("No
    ");
        }
    
        return 0;
    }
    
    
  • 相关阅读:
    MySQL query_cache_type 详解
    MySQL validate_password 插件
    MySQL冷备份的跨操作系统还原
    MySQL5.7新特性笔记
    MySQL参数详解
    保存mysql用户的登录信息到~.my.cnf文件;用于方便登录操作。
    MySQL应用层传输协议分析
    python egg
    MySQL 加锁处理分析
    train_test_split, 关于随机抽样和分层抽样
  • 原文地址:https://www.cnblogs.com/acboyty/p/11352280.html
Copyright © 2011-2022 走看看