这题挺水的,用 stack 非常好写

不过要看清题目输出格式

1
注意:当答案长度多于 4 位时,请只输出最后 4 位,前导 0 不输出。

AC 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;
stack<int> st;
int a, b;
char op;
int main()
{
cin >> a;
a = a % 10000; // 在这一定要取模
st.push(a);
while (cin >> op >> b)
{
if (op == '*')
{
a = st.top();
st.pop();
st.push(a * b % 10000);
}
else
st.push(b);
}
int ans = 0;
while (st.size())
{
ans += st.top();
ans %= 10000;
st.pop();
}
cout << ans;
return 0;
}