ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#205776 | #1691. 逆波兰表达式 | shawn | 100 | 0ms | 1236kb | C++ | 633b | 2024-07-19 18:47:41 | 2024-07-19 20:16:35 |
answer
#include <bits/stdc++.h>
using namespace std;
string s;
long long a,b,i;
stack <long long> st;
long long to_int(string c){
int j=0;
while (j<c.size()) j++;
int p=1,k=0;
while (j--){
k+=p*(c[j]-'0');
p*=10;
}
return k;
}
int main(){
while (cin>>s){
//if (!st.empty()) cout<<"the top is:"<<st.top()<<"\n";
if (s[0]=='+' || s[0]=='-' || s[0]=='*'){
b=st.top();
st.pop();
a=st.top();
st.pop();
if (s[0]=='+') st.push(a+b);
if (s[0]=='-') st.push(a-b);
if (s[0]=='*') st.push(a*b);
}else{
st.push(to_int(s));
}
}
cout<<st.top();
st.pop();
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 20
Accepted
time: 0ms
memory: 1232kb
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: 1228kb
input:
1 2 + 3 4 5 - * -
output:
6
result:
ok single line: '6'
Test #4:
score: 20
Accepted
time: 0ms
memory: 1232kb
input:
1 2 + 5 4 + *
output:
27
result:
ok single line: '27'
Test #5:
score: 20
Accepted
time: 0ms
memory: 1236kb
input:
35 26 - 28 5 + * 6 7 * -
output:
255
result:
ok single line: '255'