当前位置:网站首页>acwing 3302. 表达式求值
acwing 3302. 表达式求值
2022-07-04 19:13:00 【_刘小雨】
给定一个表达式,其中运算符仅包含 +,-,*,/(加 减 乘 整除),可能包含括号,请你求出表达式的最终值。
注意:
数据保证给定的表达式合法。
题目保证符号 - 只作为减号出现,不会作为负号出现,例如,-1+2,(2+2)*(-(1+1)+2) 之类表达式均不会出现。
题目保证表达式中所有数字均为正整数。
题目保证表达式在中间计算过程以及结果中,均不超过 231−1。
题目中的整除是指向 0 取整,也就是说对于大于 0 的结果向下取整,例如 5/3=1,对于小于 0 的结果向上取整,例如 5/(1−4)=−1。
C++和Java中的整除默认是向零取整;Python中的整除//默认向下取整,因此Python的eval()函数中的整除也是向下取整,在本题中不能直接使用。
输入格式
共一行,为给定表达式。
输出格式
共一行,为表达式的结果。
数据范围
表达式的长度不超过 105。
输入样例:
(2+2)*(1+1)
输出样例:
8
关键:
需要用两个栈, 一个栈用于保存数字,另一个需要保存操作运算符
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;
stack<int> num; // 保存数字栈
stack<char> op; // 保存运算符栈
void eval()
{
auto b = num.top(); num.pop(); // 取数字
auto a = num.top(); num.pop(); // 取数字
auto c = op.top(); op.pop(); // 取运算符
int x;
// 计算
if (c == '+') x = a + b;
else if (c == '-') x = a - b;
else if (c == '*') x = a * b;
else x = a / b;
num.push(x);
}
int main()
{
unordered_map<char, int> pr{
{
'+', 1}, {
'-', 1}, {
'*', 2}, {
'/', 2}}; // 如果有^, 可以直接在后面加 {'^',3}
string str;
cin >> str;
for (int i = 0; i < str.size(); i ++ )
{
auto c = str[i];
if (isdigit(c)) // 数字入栈
{
int x = 0, j = i;
while (j < str.size() && isdigit(str[j]))
x = x * 10 + str[j ++ ] - '0';
i = j - 1;
num.push(x);
}
else if (c == '(') op.push(c);
else if (c == ')')
{
while (op.top() != '(') eval(); // 一直需要计算到左括号
op.pop();
}
else
{
while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval(); // 栈里面保存的优先级 >= 当前的优先级
op.push(c);
}
}
while (op.size()) eval();
cout << num.top() << endl;
return 0;
}
边栏推荐
- 阿里测试师用UI自动化测试实现元素定位
- 关于联邦学习和激励的相关概念(1)
- LeetCode 871. Minimum refueling times
- Practical examples of node strong cache and negotiation cache
- idea配置标准注释
- C # better operation mongodb database
- Talking about cookies of client storage technology
- PermissionError: [Errno 13] Permission denied: ‘data.csv‘
- 同事的接口文档我每次看着就头大,毛病多多。。。
- Taishan Office Technology Lecture: about the order of background (shading and highlighting)
猜你喜欢

Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines

Automatic generation of interface automatic test cases by actual operation

C server log module

Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world
![[Beijing Xunwei] i.mx6ull development board porting Debian file system](/img/46/abceaebd8fec2370beec958849de18.jpg)
[Beijing Xunwei] i.mx6ull development board porting Debian file system

What if win11u disk refuses access? An effective solution to win11u disk access denial

【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生

针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS

Selected review | machine learning technology for Cataract Classification / classification

【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
随机推荐
实践示例理解js强缓存协商缓存
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
Dynamic memory management
Huawei cloud store homepage banner resource bit application
Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem
《动手学深度学习》(三) -- 卷积神经网络 CNN
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
最长的可整合子数组的长度
Lingyun going to sea | 10 jump &huawei cloud: jointly help Africa's inclusive financial services
Implementation of redis distributed lock
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Related concepts of federal learning and motivation (1)
Practice examples to understand JS strong cache negotiation cache
分析伦敦银走势图的技巧
Reinforcement learning - learning notes 2 | value learning
实操自动生成接口自动化测试用例
Function analysis and source code of hash guessing game system development
What is the development of block hash quiz game system? Hash quiz game system development (case mature)
左右最值最大差问题