ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#205749 | #1691. 逆波兰表达式 | cql | 100 | 1ms | 1228kb | C++11 | 739b | 2024-07-19 18:22:57 | 2024-07-19 20:11:26 |
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
int fn(char c, int a, int b) {
if (c == '+')
return a + b;
if (c == '-')
return a - b;
if (c == '*')
return a * b;
}
inline bool isNum(char c) {
return c >= '0' && c <= '9';
}
signed main() {
string s;
getline(cin, s);
stack<int> stk;
for (int i = 0; i < s.size(); i += 2) {
for (int sum, p = 1; isNum(s[i]) || s[i] == '-' && isNum(s[i + 1]); stk.push(sum * p), i++, p = 1) {
if (s[i] == '-')
p = -1, i++;
for (sum = 0; isNum(s[i]); i++)
sum = sum * 10 + s[i] - '0';
}
int b = stk.top();
stk.pop();
int a = stk.top();
stk.pop();
stk.push(fn(s[i], a, b));
}
cout << stk.top();
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 20
Accepted
time: 0ms
memory: 1224kb
input:
23 456 239 + + 123 874 908 345 * - + + 23 44 664 * - +
output:
-340738
result:
ok single line: '-340738'
Test #2:
score: 20
Accepted
time: 0ms
memory: 1228kb
input:
1 2 3 4 - * + 5 6 * -
output:
-31
result:
ok single line: '-31'
Test #3:
score: 20
Accepted
time: 0ms
memory: 1224kb
input:
1 2 + 3 4 5 - * -
output:
6
result:
ok single line: '6'
Test #4:
score: 20
Accepted
time: 1ms
memory: 1224kb
input:
1 2 + 5 4 + *
output:
27
result:
ok single line: '27'
Test #5:
score: 20
Accepted
time: 0ms
memory: 1224kb
input:
35 26 - 28 5 + * 6 7 * -
output:
255
result:
ok single line: '255'