ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

# 华为机试:HJ77火车进站 与 HJ50四则运算

2022-08-08 16:31:18  阅读:517  来源: 互联网

标签:nums int st HJ77 num push 机试 HJ50 op


华为机试

记录两个与栈相关的算法题,折腾了一下午

  • 需要注意dfs前后对称的操作
  • 利用栈结构去处理存在明显先后顺序的问题,比如四则运算等

HJ77 火车进站

栈+dfs回溯

#include<bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> res;
vector<int> path;
void dfs(stack<int>& st, int index, vector<int >& nums) {
    if (index == N && st.empty()) {
        res.push_back(path);
        return;
    }
    // 进栈
    int tmp = nums[index];
    if (index < nums.size()) {
        st.push(tmp);
        dfs(st, index + 1, nums);
        st.pop();
    }
    // 出栈
    if (!st.empty()) {
        path.push_back(st.top());
        st.pop();
        dfs(st, index, nums);
        st.push(path.back());
        path.pop_back();
    }
}

int main() {

    cin >> N;
    vector<int > nums(N, 0);
    for (int i = 0; i < N; ++i) {
        cin >> nums[i];
    }

    stack<int> out;
    dfs(out, 0, nums);
    sort(res.begin(), res.end(), [](vector<int> a, vector<int> b) {
        for(int i = 0; i < a.size(); ++i) {
            if (a[i] != b[i]) return a[i] < b[i];
        }
        return true;
    });

    for (auto v : res) {
        for (auto s : v)
            cout << s << " ";
        cout << endl;
    }
    return 0;
}

HJ50 四则运算

#include <bits/stdc++.h>
using namespace std;

// 比较当前操作符和栈顶操作符优先级,cur<top 返回true
bool cmp(char cur, char top) {
    if (top == '(')
        return false;
    else if ( (cur == '*' || cur == '/') && (top == '+' || top == '-') )
        return false;
    return true;
}

void calc(stack<double> &num,stack<char> &op){
    int b=num.top();num.pop();
    int a=num.top();num.pop();
    char c=op.top();op.pop();
    if(c=='+') a=a+b;
    else if(c=='-') a=a-b;
    else if (c=='*') a=a*b;
    else if(c=='/') a=a/b;
    num.push(a);
}

double eval(string s){
    stack<double> num;
    stack<char> op;
    op.push('(');
    s+=")";
    bool isNextOp=false;
    for(int i=0;i<s.size();++i){
        // 判断括号
        if(s[i]=='('||s[i]=='['||s[i]=='{'){
            op.push('(');
            continue;
        }
        if(s[i] == ')' || s[i] == ']' || s[i] == '}'){
            while(op.top()!='(')
                calc(num,op);
            op.pop();  //弹出 '('
            continue;
        }
        // 当下一个是数,不是op
        if(!isNextOp){
            int k=i;
            if(s[i]=='+'||s[i]=='-') i++;
            while(s[i]>='0'&&s[i]<='9') i++;
            double tmp=static_cast<double> (stoi(s.substr(k,i-k)));
            num.push(tmp);
            i--;
            isNextOp=true;
        }else{// 执行内部优先级
            while(cmp(s[i],op.top())){
                calc(num,op);
            }
            op.push(s[i]);
            isNextOp=false;
        }
    }
    return num.top();
}

int main() {
    string s;
    getline(cin, s);
    cout << eval(s);
    return 0;
}

标签:nums,int,st,HJ77,num,push,机试,HJ50,op
来源: https://www.cnblogs.com/chetwin/p/16562351.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有