CCF 二十四点 2019-11-25

读完题,感觉还是用栈来做最好
于是,去学习了一下栈的应用
感谢https://www.cnblogs.com/ellen-mylife/p/11110127.html

#include<stack>         //头文件
stack <int> num;      //定义一个int型栈
num.push();            //在栈顶上堆进一个元素
num.pop();             //删除掉栈顶上的元素
num.top();             //返回栈顶的元素,并不会删除  
num.empty();           //返回栈是否为空
num.size();            //返回当前栈中元素的个数
#include<iostream>
#include<stack>
using namespace std;
stack<int> num;
stack<char> sign;
char str[8];
int main()
{
    int n;
    cin>>n;
    getchar();
    while(n--)
    {
        gets(str);
        while(!num.empty()) num.pop();
        while(!sign.empty()) sign.pop();
        for(int j=0;j<sizeof(str);j++)
        {
            if(str[j]>'0'&&str[j]<='9')
            {
                num.push(str[j]-'0');
            }
            else if(str[j]=='+')
            {
                sign.push('+');
            }
            else if(str[j]=='-')
            {
                j++;
                num.push((-1)*(str[j]-'0'));
                sign.push('+');
            }
            else if(str[j]=='x')
            {
                int q=num.top();
                num.pop();
                j++;
                int p=str[j]-'0';
                num.push(q*p);
            }
            else if(str[j]=='/')
            {
                int q=num.top();
                num.pop();
                j++;
                int p=str[j]-'0';
                num.push(q/p);
            }
         } 
         while(!sign.empty())
        {
            int q=num.top();
            num.pop();
            int p=num.top();
            num.pop();
            sign.pop();
            num.push(q+p);
        }
        if(num.top()==24) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1-------- 走进前端 2-------- jQuery 3-------- CSS 4-------- A...
    依依玖玥阅读 2,357评论 0 34
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    小迈克阅读 3,055评论 1 3
  • 1.表单 Vuex的state不能使用v-model指令绑定到表单元素,会冲突的报错的。因为,一旦input的...
    钱罗罗_阅读 1,496评论 3 0
  • 插艾叶,摆枣花, 身着盛装,香包挂胸前, 端午佳节人团圆, 庭院融融,百花尽争艳。 吃粽子,烙韭盒, 凉面拌上,畅...
    开心果_c632阅读 186评论 2 2
  • 首先感谢封封老师 为我们带来《职业银行培训师VI形象力》这么精彩的课程,今天的内容主要为:授课的展示与呈现...
    三人行黄洁虹阅读 210评论 0 0