牛客算法周周练3 D--表达式求值(stack)

地址:https://ac.nowcoder.com/acm/contest/5338

     解析:这道题用python的话,也就几句代码的事~c++的话,用stack模拟即可,stack是先进后出,所以很符合本题要求。因为输入*,再输入一个数的话,肯定是先乘它之前最后出现的数。把乘出来的数放进stack,最后再累加一下就好了。

#include<iostream>
#include<cstdio>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
using namespace std;
int main()
{
    int a,b;
    stack<int>s;
    char ch;
    for(int i=1;;i++)
    {
        if(i%2==1)
        {
            cin>>a;
            a=a%10000;
            s.push(a);
        }
        else
        {
            ch=getchar();
            if(ch=='
')
                break;
            if(ch=='*')
            {
                cin>>a;
                b=s.top();
                s.pop();
                s.push(a*b%10000);
                i++;
            }
        }
    }
    int sum= 0 ;
    while(!s.empty())
    {
        sum=(sum+s.top())%10000;
        s.pop();
    }
    cout<<sum<<endl;
}
原文地址:https://www.cnblogs.com/liyexin/p/12818306.html