记录编号 598210 评测结果 AAAAAAAAAA
题目名称 [NOIP 2013PJ]表达式求值 最终得分 100
用户昵称 Gravatarxxz 是否通过 通过
代码语言 C++ 运行时间 0.151 s
提交时间 2025-01-22 17:17:13 内存使用 5.26 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
string s;
vector<string> ve;

int typ(string x) {
	if (x == "*") return 3;
	if (x == "+") return 2;
	return 1;
}

int tnum(string x) {
	int res = 0;
	for (int i = 0; i < x.length(); i++) res = (res * 10 + x[i] - '0') % 10000;
	return res;
}

stack<int> st;

int main() {
	freopen("expr2013.in", "r", stdin);
	freopen("expr2013.out", "w", stdout);

	cin >> s;
	string nw;
	for (int i = 0; i < s.length(); i++) {
		if (s[i] == '+' || s[i] == '*') ve.push_back(nw), ve.push_back(s.substr(i, 1)), nw = "";
		else nw += s[i];
	}
	ve.push_back(nw);

	bool nd = 0;
	for (int i = 0; i < ve.size(); i++) {
		if (typ(ve[i]) == 1) {
			st.push(tnum(ve[i]));
			if (nd) {
				int a = st.top();
				st.pop();
				int b = st.top();
				st.pop();
				st.push((a * b) % 10000);
				nd = 0;
			}
		} else if (typ(ve[i]) == 3) nd = 1;
	}
	int ans = 0;
	while (st.size()) ans = (ans + st.top()) % 10000, st.pop();
	printf("%d", ans);


	return 0;
}