CCF 201903-2 试题名称: 二十四点

15 篇文章 0 订阅

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        stack<int> nums;//参与运算的数字

        string str;//运算式
        cin>>str;
        int flag=1;//负号标志位

        for(int j=0;j<7;j++)
        {
            if(str[j]>='0' && str[j]<='9')
            {
                nums.push(flag*(str[j]-'0'));
                flag=1;//每次flag都要归位
            }
            else if(str[j]=='x')//如果是乘法,则取出前后两数相乘
            {
                int a=nums.top();
                nums.pop();
                int b=str[++j]-'0';
                nums.push(a*b);
            }
            else if(str[j]=='/')//如果是除法,则取出前后两数相乘
            {
                int a=nums.top();
                nums.pop();
                int b=str[++j]-'0';
                nums.push(a/b);
            }
            else if(str[j]=='-')//如果是减号,则把后一位置为负数
            {
                flag=-1;
            }
        }

        int sum=0;
        while(!nums.empty())
        {
            sum+=nums.top();

            nums.pop();
        }

        if(sum==24)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }

    return 0;
}

/*
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
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值