题解 | #【模板】队列#

【模板】队列

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")

全部评论

相关推荐

01-26 18:45
门头沟学院 Java
一天代码十万三:哥们实习再包一下吧,产出太笼统了,尽量体现业务
点赞 评论 收藏
分享
KPLACE:首先是板面看起来不够,有很多奖,比我厉害。项目要精减,大概详细描述两到三个,要把技术栈写清楚,分点,什么算法,什么外设,怎么优化,不要写一大堆,分点,你写上去的目的,一是让别人知道你做了这个知识点,然后在面试官技术面的时侯,他知道你会这个,那么就会跟你深挖这个,然后就是个人评价改为专业技能
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
02-16 22:33
杉川机器人 嵌入式工程师 18.0k*13.0, 年终奖1~9个月浮动
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务