求助前缀表达式求解报错了RE
查看原帖
求助前缀表达式求解报错了RE
791222
lby_commandBlock楼主2023/8/11 10:50
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

const int N = 1e5 + 9;

stack<int> k;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n; cin >> n;
	int t = 0;
	string s; getline(cin, s);
	for (int i = s.size() - 1; i >= 0; i--) {\
		char c = s[i];
		if (c >= '0' && c <= '9') t = t * 10 + (c - '0');
		if (c == ' ') {
			k.push(t); 
			t = 0;
		} else {
			int a = k.top(); k.pop(); int b = k.top(); k.pop();
			if (c == '+') k.push(a + b);
			else if (c == '-') k.push(fabs(a - b));
			else if (c == '*') k.push(a * b);
			else k.push(a / b);
		}
	}
	cout << k.top() << endl;
	return 0;
}```
2023/8/11 10:50
加载中...