当前位置:网站首页>P1449 后缀表达式
P1449 后缀表达式
2022-08-03 22:51:00 【Recursi】
题目描述
所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级)。
如: 3*(5-2)+7 \texttt{3*(5-2)+7} 3*(5-2)+7 对应的后缀表达式为: 3.5.2.-*[email protected] \texttt{3.5.2.-*[email protected]} 3.5.2.-*[email protected]。在该式中,@
为表达式的结束符号。.
为操作数的结束符号。
输入格式
输入一行一个字符串 s s s,表示后缀表达式。
输出格式
输出一个整数,表示表达式的值。
样例 #1
样例输入 #1
3.5.2.-*[email protected]
样例输出 #1
16
提示
数据保证, 1 ≤ ∣ s ∣ ≤ 50 1 \leq |s| \leq 50 1≤∣s∣≤50,答案和计算过程中的每一个值的绝对值不超过 1 0 9 10^9 109。
/* * @Description: To iterate is human, to recurse divine. * @Autor: Recursion * @Date: 2022-08-02 18:17:15 * @LastEditTime: 2022-08-02 18:39:47 */
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;
const int N = 1e6;
string s;
int a,b;
int n,m;
stack<int> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
for(int i = 0;i < s.length();i++){
if(s[i] == '@')
break;
else if(s[i] == '.'){
q.push(a);
a = 0;
b = 0;
}
else if(s[i] >= '0' && s[i] <= '9'){
a = b * 10 + s[i] - '0';
b = a;
}
else{
if(s[i] == '+'){
n = q.top();
q.pop();
m = q.top();
q.pop();
q.push(m + n);
}
if(s[i] == '-'){
n = q.top();
q.pop();
m = q.top();
q.pop();
q.push(m - n);
}
if(s[i] == '*'){
n = q.top();
q.pop();
m = q.top();
q.pop();
q.push(m * n);
}
if(s[i] == '/'){
n = q.top();
q.pop();
m = q.top();
q.pop();
q.push(m / n);
}
}
}
cout << q.top() << endl;
return 0;
}
边栏推荐
- Testng监听器
- 2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
- Testng listener
- Quickly build a website with static files
- Embedded Systems: Clocks
- Research status of target detection at home and abroad
- ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
- Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
- Kotlin - 扩展函数和运算符重载
- [MySQL Advanced] Creation and Management of Databases and Tables
猜你喜欢
Pytest learn-setup/teardown
complete binary tree problem
Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
Binary search tree to solve the fallen leaves problem
win10系统下yolov5-V6.1版本的tensorrt部署细节教程及bug修改
生成器版和查看器版有什么区别?
Republish the lab report
获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》
[b01lers2020]Life on Mars
Embedded Systems: Clocks
随机推荐
[b01lers2020]Life on Mars
HCIP BGP lab report
Click the icon in Canvas App to generate PDF and save it to Dataverse
2022-08-03 oracle执行慢SQL-Q17对比
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
软件测试内卷严重,如何提升自己的竞争力呢?
最小化安装debian11
Shell编程的条件语句
亿流量大考(2):开发一套高容错分布式系统
utils 定时器
Golang Chapter 1: Getting Started
websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
Research status of target detection at home and abroad
Embedded Systems: Clocks
complete binary tree problem
LabVIEW代码生成错误 61056
start with connect by implements recursive query
October 2019 Twice SQL Injection
Codeup刷题笔记-简单模拟