题解 | #【模板】队列#

【模板】队列

https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549?tpId=308&tqId=2110348&ru=/exam/oj&qru=/ta/algorithm-start/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D308

#include <iostream>
using namespace std;
#include<algorithm>
#include <string>
class stack{
    private:    
        int s[100000];
        int top=-1;
    public:
        void push(int x)
        {
            top++;
            s[top]=x;
        }
        void pop()
        {
            // if(top>=0)
            // {
            //     if(top==0)
            //     {
            //         cout<<s[0]<<endl;
            //         top=-1;
            //     }
            //     else
            //     {
            //         cout<<s[0]<<endl;
            //         reverse(s, s+top); //反转
            //         top--;
            //         reverse(s, s+top); //反转    
            //     }
            // }
            if(top>=0)
            {
                cout<<s[0]<<endl;
                for(int i=0;i<top;i++)
                {
                    s[i]=s[i+1];
                }
                    top--;
            }
            else cout<<"error"<<endl;           
        }
        void front()
        {
            if(top>=0)
            {
                cout<<s[0]<<endl;
            }
            else cout<<"error"<<endl;
        }
};

int main() {
    // int a, b;
    // while (cin >> a >> b) { // 注意 while 处理多个 case
    //     cout << a + b << endl;
    // }
    stack s;
    int n=0;
    cin >>n;
    for(int i=0;i<n;i++)
    {
        string op;
        cin>>op;
        if(op=="push")
        {
            int a=0;
            cin>>a;
            s.push(a);
        }
        if(op=="pop")
        {
            s.pop();
        }
        if(op=="front")
        {
            s.front();
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 17:28
25届每天都在焦虑找工作的事情0offer情绪一直很低落硬撑着面了一个岗位岗位有应酬的成分面试的时候hr给我出各种场景题问的问题比较犀利&nbsp;有点压力面的感觉感觉有点回答不上来本来就压抑的情绪瞬间爆发了呢一瞬间特别想哭觉得自己特别没用没绷住掉眼泪了事后想想觉得自己挺有病的&nbsp;真的破大防了
喜欢唱跳rap小刺猬...:我觉得没关系吧,之前有一次面试leader给我压力面,我顶住了压力,结果入职的时候发现组里氛围很差,果断跑路。其实从面试就能大概看出组的情况,面试体验好的组倒是不一定好,但是面试体验不好的组。。。就很难说
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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