ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#205759 | #1691. 逆波兰表达式 | Soulmate | 100 | 0ms | 1204kb | C++11 | 1.2kb | 2024-07-19 18:32:32 | 2024-07-19 20:13:57 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
stack<int> p;
signed main()
{
char c;
int n=0;
bool f=false;
while ((c=getchar())!=EOF)
{
if (isdigit(c)) {
f=true;
n=n*10+(c-'0');
} else if (c==' '&&f)
{
p.push(n);
n=0;
f=false;
} else if (c=='+'||c=='-'||c=='*')
{
if (f)
{
p.push(n);
n=0;
f=false;
}
if (!p.empty()&&p.size()>=2)
{
int b=p.top();
p.pop();
int a=p.top();
p.pop();
if (c=='+')
{
p.push(a + b);
}
else if (c=='-')
{
p.push(a-b);
}
else if (c=='*')
{
p.push(a * b);
}
}
}
}
if (!p.empty())
{
cout<<p.top()<<endl;
}
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 20
Accepted
time: 0ms
memory: 1200kb
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: 1204kb
input:
1 2 3 4 - * + 5 6 * -
output:
-31
result:
ok single line: '-31'
Test #3:
score: 20
Accepted
time: 0ms
memory: 1200kb
input:
1 2 + 3 4 5 - * -
output:
6
result:
ok single line: '6'
Test #4:
score: 20
Accepted
time: 0ms
memory: 1204kb
input:
1 2 + 5 4 + *
output:
27
result:
ok single line: '27'
Test #5:
score: 20
Accepted
time: 0ms
memory: 1200kb
input:
35 26 - 28 5 + * 6 7 * -
output:
255
result:
ok single line: '255'