【NOI题解】 3.3数据结构之栈

NOI题目地址( 3.3数据结构之栈 )http://noi.openjudge.cn/ch0303/
这里写图片描述

1696:逆波兰表达式
分析:
逆波兰表达式计算

用一个栈来存储数字,从右向左遍历表达式

  • 遇到数字就压栈
  • 遇到操作符就弹出栈中的两个数字计算并压栈

最后栈中剩余的数字就是我们要的结果
- Tip
这里计算的是前缀表达式,如果计算后缀表达式,将从右向左遍历改为从左向右遍历,修改带有次序的运算符如’/’的运算数的顺序(入栈次序与前序正好相反)。

AC题解:
#include <stack>
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void cal(string param, stack<double> &num);

void bolan(string &param, stack<double> &num)
{
    // 遇到数字就入栈
    if (param != "+"&&param != "*"&&param != "-"&&param != "/")
    {
        num.push(atof(param.c_str()));
    }
    // 遇到操作符就计算,并将结果入栈
    else
    {
        cal(param, num);
    }
}
void cal(string param, stack<double> &num)
{
    double num1 = num.top();
    num.pop();
    double num2 = num.top();
    num.pop();

    if (param == "+")
    {
        num.push(num1 + num2);
    }
    if (param == "-")
    {
        num.push(num1 - num2);
    }
    if (param == "*")
    {
        num.push(num1 * num2);
    }
    if (param == "/")
    {
        num.push(num1 / num2);
    }
    // cout<<"cal result:"<<num.top()<<endl;
}
int main()
{
    stack<double> num;
    string command;
    getline(cin, command);

    int index1 = 0, index2 = command.length()-1;
    // 从右向左遍历表达式 获取操作符和数字
    for (int i = command.length()-1; i>=0; i--)
    {
        if (command[i] == ' ' || i == 0)
        {
            if(i==0) index1 = 0; else index1 = i+1;
            string param = command.substr(index1, index2-index1+1);
            // cout << index1 << " " << index2 << " #" << param << endl;
            index2 = i-1;
            bolan(param, num);
        }

    }
    cout<<fixed<<setprecision(6)<<num.top()<<endl;
}

3340:RPN Calculator
分析:
题意
  1. 有一串初始值,称之为memory
  2. 每次计算出后缀表达式的值,替换掉这串初始值中的最小值
  3. 按照每行10个数的格式输出memory中的值
难点
  • 后缀表达式的计算
    用一个栈来存储数字,从左向左右遍历表达式
    遇到数字就压栈
    遇到操作符就弹出栈中的两个数字计算并压栈
    最后栈中剩余的数字就是我们要的结果

  • 如何高效的找到最小值并替换?
    这里使用了C++ STL priority_queue(优先队列),优先队列本质是一个堆,每次操作堆顶即可。

AC题解:
#include <stack>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
using namespace std;
void cal(string param, stack<double> &num);

void bolan(string &param, stack<double> &num)
{
    
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值