当前位置:网站首页>1331:【例1-2】后缀表达式的值
1331:【例1-2】后缀表达式的值
2022-07-08 00:57:00 【一位13岁的编程爱好者】
题目
1331:【例1-2】后缀表达式的值
时间限制: 10 ms 内存限制: 65536 KB
【题目描述】
从键盘读入一个后缀表达式(字符串),只含有0-9组成的运算数及加(+)、减(—)、乘(*)、除(/)四种运算符。每个运算数之间用一个空格隔开,不需要判断给你的表达式是否合法。以@作为结束标志。
比如,16–9*(4+3)转换成后缀表达式为:16□9□4□3□+*–,在字符数组A中的形式为:
栈中的变化情况:
运行结果:-47
提示:输入字符串长度小于250,参与运算的整数及结果之绝对值均在264范围内,如有除法保证能整除。
【输入】
一个后缀表达式。
【输出】
一个后缀表达式的值。
【输入样例】
16 9 4 3 +*[email protected]
【输出样例】
-47
题目分析:开一个类型为long long的栈,遇到数字就压栈,遇到符号放出栈顶两个元素,计算后再压栈即可。最后栈顶元素即为所求。
C++代码
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<long long> st;
char ch;
long long n = 0;
while ((ch = getchar()) != '@')
{
if (ch >= '0' && ch <= '9')
n = n * 10 + ch - '0';
else if (ch == ' ')
{
st.push(n);
n = 0;
}
else
{
long long a = st.top();
st.pop();
long long b = st.top();
st.pop();
//注意,由于栈先进后出的特性,减法和除法时一定要把b放到前面!
if (ch == '+')
st.push(b + a);
else if (ch == '-')
st.push(b - a);
else if (ch == '*')
st.push(b * a);
else if (ch == '/')
st.push(b / a);
}
}
cout << st.top();
return 0;
}
边栏推荐
- leetcode 865. Smallest Subtree with all the Deepest Nodes | 865. The smallest subtree with all the deepest nodes (BFs of the tree, parent reverse index map)
- Vim 字符串替换
- Unity 射线与碰撞范围检测【踩坑记录】
- Leetcode featured 200 channels -- array article
- leetcode 865. Smallest Subtree with all the Deepest Nodes | 865.具有所有最深节点的最小子树(树的BFS,parent反向索引map)
- Redisson分布式锁解锁异常
- 发现值守设备被攻击后分析思路
- The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
- Deeppath: a reinforcement learning method of knowledge graph reasoning
- 《ClickHouse原理解析与应用实践》读书笔记(7)
猜你喜欢
Completion report of communication software development and Application
Nacos microservice gateway component +swagger2 interface generation
JVM memory and garbage collection-3-object instantiation and memory layout
LeetCode精选200道--数组篇
[knowledge map paper] attnpath: integrate the graph attention mechanism into knowledge graph reasoning based on deep reinforcement
[knowledge map paper] r2d2: knowledge map reasoning based on debate dynamics
《通信软件开发与应用》课程结业报告
Industrial Development and technological realization of vr/ar
adb工具介绍
Ml self realization / logistic regression / binary classification
随机推荐
Introduction to ADB tools
Anan's judgment
发现值守设备被攻击后分析思路
[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
Introduction to Microsoft ad super Foundation
Mqtt x newsletter 2022-06 | v1.8.0 release, new mqtt CLI and mqtt websocket tools
牛熊周期与加密的未来如何演变?看看红杉资本怎么说
#797div3 A---C
leetcode 866. Prime Palindrome | 866. 回文素数
burpsuite
Direct addition is more appropriate
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
Talk about the realization of authority control and transaction record function of SAP system
微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“
QT -- create QT program
The circuit is shown in the figure, r1=2k Ω, r2=2k Ω, r3=4k Ω, rf=4k Ω. Find the expression of the relationship between output and input.
线程死锁——死锁产生的条件
PHP calculates personal income tax