AI 生成摘要
本题利用栈处理算术运算,关键在于结果均需对 10000 取模。输入时先将初始数字取模并存入栈,后续遇到乘法时弹出相乘再回模后压入;遇到加法则直接压入。最后累加栈中所有元素并持续取模,输出最终结果。特别注意输出格式:若结果长度超过 4 位,仅输出后 4 位且去除前导零。

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

不过要看清题目输出格式

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

AC 代码

#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 << "\n";
	return 0;
}