0,必关

B3614 【模板】栈

michaelwanghaoyu @ 2024-12-14 15:43:29

新接触stack 求救

#include<iostream>
#include<stack> 
using namespace std;
int main()
{
    int x,n,m;
    string cz; 
    cin>>n;
    for (int i=0;i<n;i++)
    {
        stack<int>q;
        cin>>m;
        for (int ii=0;ii<m;ii++)
        {
            cin>>cz;
            if (cz=="push")
            {
                cin>>x;
                q.push(x);
            }
            else if (cz=="pop")
            {
                if (!q.empty())
                {
                    q.pop();
                }
                else
                {
                    cout<<"Empty";
                }
            }
            else if (cz=="query")
            {
                if (!q.empty())
                {
                    cout<<q.top()<<endl;
                }
                else
                {
                    cout<<"Anguei!"<<endl;
                }
            }
            else
            {
                cout<<q.size()<<endl;
            }
        }
    }
    return 0;
}

by Allen15 @ 2024-12-14 15:49:59

what is ii?@michaelwanghaoyu


by Allen15 @ 2024-12-14 15:51:17

also query(),push(x),pop(),size()


by suhaoyuan @ 2024-12-14 15:54:26


#include<bits/stdc++.h>
using namespace std;
stack <unsigned long long int> q;
int n,b;
unsigned long long int a;
string s;
signed main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>b;
        for(int i=1;i<=b;i++)
        {
            cin>>s;
            if(s=="push")
            {
                cin>>a;
                q.push(a);
            }
            else if(s=="pop")
            {
                if(q.empty()) cout<<"Empty"<<endl;
                else q.pop();
            }
            else if(s=="query")
            {
                if(q.empty()) cout<<"Anguei!"<<endl;
                else cout<<q.top()<<endl;
            }
            else cout<<q.size()<<endl;
        }
        while(q.size()) q.pop();
    }
    return 0;
}

这是我的代码


by xiao_queen @ 2024-12-14 15:55:18

数据范围请注意为

2的64次方,所以请开unsigned long long

#include<iostream>
#include<stack> 
using namespace std;
signed main()
{
    unsigned long long x,n,m;
    string cz;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        stack<unsigned long long>q;
        cin>>m;
        for (int ii=0;ii<m;ii++)
        {
            cin>>cz;
            if (cz=="push")
            {
                cin>>x;
                q.push(x);
            }
            else if (cz=="pop")
            {
                if (!q.empty()) q.pop();
                else cout<<"Empty"<<endl;
            }
            else if (cz=="query")
            {
                if (!q.empty()) cout<<q.top()<<endl;
                else cout<<"Anguei!"<<endl;
            }
            else cout<<q.size()<<endl;
        }
    }
    return 0;
}

by michaelwanghaoyu @ 2024-12-14 15:56:06

@Allen15 命令数啊

不喜欢用j


by xiao_queen @ 2024-12-14 15:56:38

@michaelwanghaoyu


by michaelwanghaoyu @ 2024-12-14 16:08:05

@xiao_queen 关注了,谢谢


|