0
点赞
收藏
分享

微信扫一扫

第五章向量、队列、栈

zhaoxj0217 2022-03-16 阅读 82
算法

1.向量vector 

主要介绍构造、操作

(1)构造

(2)操作

 

2.队列queue(特性,先进先出)

在机试考试中,我们不必自己写一个队列,系统已经封装好了队列这种数据结构

(1)基本操作:

 (2)基本操作代码示范:

(3)例题:猫狗收容所

3.栈stack(特性,后进先出)

在机试考试中,我们不必自己写一个栈,系统已经封装好了栈这种数据结构

(1)基本操作:

(2)基本操作代码示范:

(3)栈的应用

 

逆序输出

例题:Zero-complexity Transposition(来自牛客网)

#include<iostream>
#include<stack>
using namespace std;
int main() {
    int n;
    long long number;
    stack<long long> s;
    while(cin >> n){
        for(int i = 0; i < n; i ++){
            cin >> number;
            s.push(number);
        }
        
        for(int i = 0; i < n; i ++){
            cout << s.top() << " ";
            s.pop();
        }
    }
    return 0;
}

括号匹配

例题:括号匹配问题(来自?)

#include<iostream>
#include<stack>
#include<vector>
using namespace std;
int main() {
    string str;
    while(getline(cin , str)){
        stack<char> s;
        vector<char> v;
        for(int i = 0; i < str.length(); i ++){
            cout << str[i];
            if(str[i] == '('){
                if(i == str.length() - 1){
                    v.push_back('$');
               }else{
                    s.push(str[i]);
                    v.push_back(' ');
               }
            }else if(str[i] == ')'){
                if(s.empty()){
                    v.push_back('?');
                }else if(s.top() == '('){
                    v.push_back(' ');
                    s.pop();
                }
            }else{
                v.push_back(' ');
            }
        }
        cout << endl;
        for(int j = 0; j < v.size(); j ++){
            cout << v[j];
        }
        cout << endl;

    }
    return 0;
}

表达式求值

例题:简单计算器(题目来自牛客网)

思路,分两个栈,一个栈保存运算符,一个保存运算数,保存运算符的规则就是保存运算符前要把比当前指向的运算符优先运算的运算符弹出栈(可能要弹好几个)。

#include<iostream>
#include<stack>
#include<unordered_map>
using namespace std;
unordered_map<char,int> map = {{'#',0},{'+',1},{'-',1},{'*',2},{'/',2}};

double calculate(double n1,char c,double n2){
    if(c == '+'){
        return n1 + n2;
    }
    if(c == '-'){
        return n1 - n2;
    }
    if(c == '*'){
        return n1 * n2;
    }
    if(c == '/'){
        return n1 / n2;
    }
    return 0;
}


int main() {
    string str;
    while(getline(cin , str)){
        stack<char> c;//放运算符
        stack<double> n;//放运算数
        double n1=0;
        double result;
        c.push('#');
        for(int i = 0; i < str.length(); i ++){
            if(i == str.length() - 1){
                n.push(n1 * 10 + str[i] - '0');
                n1=0;
               while(c.top() != '#'){
                    char c1 = c.top();
                    c.pop();
                    double t2 = n.top();
                    n.pop();
                    double t1 = n.top();
                    n.pop();
                    n.push(calculate(t1,c1,t2));
               }
               result = n.top();
            }else if(str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/'){
                while( map[c.top()] >= map[str[i]] ){
                    double t2 = n.top();
                    n.pop();
                    double t1 = n.top();
                    n.pop();
                    char c1 = c.top();
                    n.push(calculate(t1 , c1 , t2));
                    c.pop();
                }
                c.push(str[i]);

            }else if('0' <= str[i] && str[i] <= '9'){
                n1 = n1 * 10 + str[i] - '0';
                if(!('0' <= str[i+1] && str[i+1] <= '9')){
                    n.push(n1);
                    n1 = 0;
                }
            }
        }
        printf("%.2lf\n",result);
    }
    return 0;
}
举报

相关推荐

0 条评论