1 条题解

  • 0
    @ 2023-07-20 19:56:09
    /*
    给没去过冬令营的同学露一手
    栈(stack)定义方式如下:
    stack<类型>名称;
    常用的函数:
    名称.top()取栈顶元素
    名称.pop()弹出栈顶元素
    名称.push(x)将x压入栈里
    名称.empty()判断该栈是否为空,空了返回true,未空返回false
    
    此题仅需了解后缀表达式,再结合char的相关知识即可解答
    */
    #include<bits/stdc++.h>
    using namespace std;
    stack<long long> s;
    long long now=0;
    char op;
    int main()
    {
        op=getchar();
        while(op!='@')
        {
            if(op>='0'&&op<='9')
                now*=10,now+=op-'0';
            else if(op=='.')
            {
                s.push(now);
                now=0;
            }
            else if(op=='+')
            {
                int x=s.top();s.pop();
                int y=s.top();s.pop();
                s.push(x+y);
            }
            else if(op=='-')
            {
                int x=s.top();s.pop();
                int y=s.top();s.pop();
                s.push(y-x);
            }
            else if(op=='*')
            {
                int x=s.top();s.pop();
                int y=s.top();s.pop();
                s.push(x*y);
            }
            else if(op=='/')
            {
                int x=s.top();s.pop();
                int y=s.top();s.pop();
                s.push(y/x);
            }
            op=getchar();
        }
        cout<<s.top();
        return 0;
    }
    
  • 1

信息

ID
1662
难度
9
分类
(无)
标签
递交数
9
已通过
4
通过率
44%
被复制
4
上传者