听取蛙声一个
查看原帖
听取蛙声一个
1046508
Sparse_Table楼主2024/8/2 12:53

麻烦回复时 @ 我一下谢谢。

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

stack<char> op;
stack<int> num;
void calc() {
	int y = num.top();
	num.pop();
	int x = num.top();
	num.pop();
	char c = op.top();
	op.pop();
	if (c == '+') num.push((x + y) % 10000);
	else num.push(x * y % 10000);
}

int main() {
	string s;
	cin >> s;
	int t = 0;
	for (int i = 0; i < s.size(); i++) {
		if (isdigit(s[i])) t = t * 10 + s[i] - 48;
		else if (s[i] == '+') {
			num.push(t);
			t = 0;
			while (!op.empty()) calc();
			op.push(s[i]);
		} else {
			num.push(t);
			t = 0;
			while (!op.empty() && op.top() == '*') calc();
			op.push(s[i]);
		}
	}
	num.push(t);
	while (!op.empty()) calc();
	cout << num.top();
	return 0;
}
2024/8/2 12:53
加载中...