ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#205768 | #1691. 逆波兰表达式 | kevin0625 | 100 | 1ms | 1208kb | C++ | 747b | 2024-07-19 18:42:09 | 2024-07-19 20:15:40 |
answer
#include<bits/stdc++.h>
using namespace std;
stack<long long int> sta;
char c[10001000];
int main()
{
gets(c);
for(int i=0;c[i]!='\0';i++)
{
if(c[i]!='+'&&c[i]!='-'&&c[i]!='*'&&c[i]!=' ')
{
int a=int(c[i]-'0');
i++;
while(c[i]!=' ')
{
a*=10;
a+=int(c[i]-'0');
i++;
}
sta.push(a);
}
else if(c[i]=='+')
{
int a=sta.top();
sta.pop();
int b=sta.top();
sta.pop();
sta.push(a+b);
}
else if(c[i]=='-')
{
int a=sta.top();
sta.pop();
int b=sta.top();
sta.pop();
sta.push(b-a);
}
else if(c[i]=='*')
{
int a=sta.top();
sta.pop();
int b=sta.top();
sta.pop();
sta.push(a*b);
}
}
cout<<sta.top();
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 20
Accepted
time: 0ms
memory: 1208kb
input:
23 456 239 + + 123 874 908 345 * - + + 23 44 664 * - +
output:
-340738
result:
ok single line: '-340738'
Test #2:
score: 20
Accepted
time: 1ms
memory: 1204kb
input:
1 2 3 4 - * + 5 6 * -
output:
-31
result:
ok single line: '-31'
Test #3:
score: 20
Accepted
time: 0ms
memory: 1204kb
input:
1 2 + 3 4 5 - * -
output:
6
result:
ok single line: '6'
Test #4:
score: 20
Accepted
time: 0ms
memory: 1208kb
input:
1 2 + 5 4 + *
output:
27
result:
ok single line: '27'
Test #5:
score: 20
Accepted
time: 0ms
memory: 1208kb
input:
35 26 - 28 5 + * 6 7 * -
output:
255
result:
ok single line: '255'