33分超时,求调

B3614 【模板】栈

liuruihang2027 @ 2024-09-16 21:04:11

求助各位大佬,程序超时了

#include<bits/stdc++.h>
using namespace std;
stack <int> st;
int n,x,m,y;
string chos;
int main() {
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        cin>>x;
        for (int i=1;i<=x;i++)
        {
            cin>>chos;
            if (chos=="push")
            {
                cin>>y;
                st.push (y);
            }
            else if (chos=="pop")
            {
                if (st.empty())
                    cout<<"Empty"<<endl;
                else    
                    st.pop();
            }
            else if (chos=="query")
            {
                if (st.empty())
                    cout<<"Anguei!"<<endl;
                else    
                    cout<<st.top()<<endl;
            }
            else
                cout<<st.size()<<endl;
        }
        while (!st.empty())
            st.top();
    }
    return 0;
}

by zjpwdyf @ 2024-09-16 21:10:19

while (!st.empty())
    st.top();

改为:

while (!st.empty())
    st.pop();

by liuruihang2027 @ 2024-09-16 21:16:32

看清了,感谢感谢 @zjpwdyf


by liuruihang2027 @ 2024-09-16 21:20:32

还是33分,现在又成WA了

#include<bits/stdc++.h>
using namespace std;
stack <int> st;
int n,x,m,y;
string chos;
int main() {
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        cin>>x;
        for (int i=1;i<=x;i++)
        {
            cin>>chos;
            if (chos=="push")
            {
                cin>>y;
                st.push (y);
            }
            else if (chos=="pop")
            {
                if (st.empty())
                    cout<<"Empty"<<endl;
                else    
                    st.pop();
            }
            else if (chos=="query")
            {
                if (st.empty())
                    cout<<"Anguei!"<<endl;
                else    
                    cout<<st.top()<<endl;
            }
            else
                cout<<st.size()<<endl;
        }
        while (!st.empty())
            st.pop();
    }
    return 0;
}

by ENJOuYang @ 2024-09-16 21:36:46

@liuruihang2027


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

by liuruihang2027 @ 2024-09-16 21:45:31

感谢感谢@ENJOuYang


|