题解 | #【模板】栈#

【模板】栈

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

#include <cmath>
#include <iostream>
#include <string>
using namespace std;

void top(int *st, int* &st_top){
    if (st == st_top) {
        cout<<"error"<<endl;
    }else {
        cout<<*st_top<<endl;
    } 
}

void push(int * st, int* &st_top, int num){
    st_top++;
    *st_top=num;
}

void pop(int *st, int* &st_top){
    if (st != st_top) {
        top(st, st_top);
        st_top--;
    }else {
        cout<<"error"<<endl;
    }
}


int main() {
    int n, i=0;
    int st[100000];
    int *st_top = st;
    string s;int num;
    cin >> n;
    getline(cin, s);
    while (i<n) {
        getline(cin, s);
        // cout<<s<<endl;
        num = 0;
        int pos = s.find(' ');
        if(pos==4){
            
            int k=s.substr(5,s.size()).size();
            
            for(auto x: s.substr(5,s.size())){
                num = num + (int)(x -'0')*pow(10, k-1);
                k--;
                // cout<<(int)(x -'0')<<" "<<num<<endl;
            }
            
            push(st, st_top, num);
        }else {
            if (s=="pop") {
                pop(st, st_top);
            }else {
                top(st, st_top);
            }
        }
        // cout<<s<<" "<<pos<<endl;
        i++;
    }

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

已经入职数字马力4个月了,忍不住想和大家聊聊最真实的感受!🔥1️⃣&nbsp;岗位偏见?作为蚂蚁的子公司,很多人会担心“内包”身份会不会有岗位偏见。就我这几个月的体验来说,数字马力一直在快速扩招,面试流程也越来越规范(尤其是校招环节)。至于偏见问题,真的看部门和leader,很幸运我遇到的师兄和主管都特别nice,团队氛围很融洽。2️⃣&nbsp;待遇怎么样?试用期工资不打折!这点我真的吹爆💥!每天六点下班还有餐补,公积金按全额8%交(感动哭)……不过养老金也是实打实的8%,到手稍微心疼一下下😂3️⃣&nbsp;技术栈跟得上吗?技术栈多到学不完……而且我们有权限访问蚂蚁的知识库,自学能力强+愿意钻研的话,成长速度真的飞快!(当然,像我这种偶尔偷懒的也在慢慢进步中😝)4️⃣&nbsp;面试流程?一般是三面:两轮技术面(可能有线上笔试)+&nbsp;一轮HR面(含背调)。整体节奏比较顺畅,反馈也及时。5️⃣&nbsp;未来发展怎么看?老实说,数字马力不算头部大厂,不能指望它给简历镀金,但也绝不是那种会“减分”的外包。我更愿意把它看作一个扎实的中厂跳板,适合积累实战经验。6️⃣&nbsp;怎么投递?通过数字马力gzh,今天刚放出一批新HC!如果你正在看机会,不妨试试数字马力~之前面挂过也没关系,不妨再战一次,机会说不定就来了!🤝✅&nbsp;我的专属内推码:NTA6Nvs,可以直接帮大家推进流程。📮&nbsp;有任何关于公司、岗位、面试的问题,也欢迎留言,我会尽量回复~(小声说:大环境不易,希望大家都能找到心仪的工作,也欢迎来找我内推呀!)
数字马力公司福利 22人发布
点赞 评论 收藏
分享
11-03 18:50
门头沟学院 Java
迷茫的大四🐶:问就是马上到,一周五天,6个月以上,全国可飞
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务