NOIP2013普及组第二题_表达式求值

//读入fu
#include <bits/stdc++.h>
using namespace std;
stack<int> n;
stack<char> op;
int main() {
    //freopen("expr.in","r",stdin);
    int x,y,i,j,tmp;
    long long ans=0;
    char ch;
    
    cin>>y;n.push(y);
    
    while(cin>>ch){
        op.push(ch);
        cin>>y;        
        if(op.top()=='*'){//消去前面的数和*
            int x=n.top();n.pop();
            tmp=x*y%10000;
            n.push(tmp);
        }else{//‘+’
            n.push(y);
        }    
    }
    while(!n.empty()){
        ans+=n.top();n.pop();
        ans%=10000;
    }    
    cout<<ans%10000;
    return 0;
}

 

 

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100000 + 10;

int num[MAXN], top;
int ans;

int main()
{
//    freopen("expr.in","r",stdin);
//    freopen("expr.out","w",stdout);
    
    int x;
    cin >> x;
    num[++top] = x;
    
    char ch;
    while(cin >> ch >> x)
    {
        if(ch == '*') num[top] = num[top] * x%10000;    
        if(ch == '+') num[++top] = x;
    }    
    
    while(top) ans += num[top--] % 10000;
    
    cout << ans%10000 << endl;
    
    return 0;
}

 

posted @ 2021-10-05 12:01  木窗  阅读(475)  评论(0)    收藏  举报