33分求调(玄关)

B3614 【模板】栈

_qumingnan_ @ 2024-10-20 17:26:26

#include<bits/stdc++.h>
using namespace std;
string s;
int t,n;
long long x,a[1000005],tot,top;
int main(){
    //freopen("栈.txt","w",stdout);
    cin>>t;
    while(t--){
        top=1;
        tot=0;
        cin>>n;
        while(n--){
            cin>>s;
            if(s=="push"){
                cin>>x;
                ++tot;
                a[tot]=x;
            }
            else if(s=="pop"){
                if(tot-top+1<=0)cout<<"Empty\n";
                else top++;
            }
            else if(s=="query"){
                if(tot-top+1<=0)cout<<"Anguei!\n";
                else cout<<a[top]<<endl;
            }
            else if(s=="size")cout<<tot-top+1<<endl;
            /*for(int i=tot;i<=top;i++){
                cout<<a[i]<<' '
            }
            cout<<tot<<' '<<top<<endl;*/
        }
    }
    return 0;
}

by _qumingnan_ @ 2024-10-20 17:30:37

long long改unsigned long long一样的33


by wangyihan_ @ 2024-10-20 17:31:24

借楼问 60pts

#include<bits/stdc++.h>
using namespace std;
int n,t;
unsigned long long x;
string st;
stack<unsigned long long int>s;
int main() 
{
    cin>>t;
    while(t--) 
    {
        cin>>n;
        while(n--)
        {
            cin>>st;
            if(st=="push") 
            {
                cin>>x; 
                s.push(x);
            } 
            else if(st=="pop") 
            {
                if(s.empty()) puts("Empty");
                else s.pop();
            } 
            else if(st=="query") 
            {
                if(s.empty()) puts("Anguei!");
                else cout<<s.top()<<'\n';
            } 
            else cout<<s.size()<<'\n';
        }
    }
    return 0;
}

by wangyihan_ @ 2024-10-20 17:31:57

@wangyihan_ 是66pts


by zhuyucheng6046 @ 2024-10-20 17:33:47

@quminnan 你可以参考一下

#include<bits/stdc++.h>
using namespace std;
unsigned long long  y[1000000+10];
int sum;string a;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        sum=0;
        for(int i=1;i<=n;i++)
        {

            cin>>a;
            if(a=="push")
            {
                 unsigned long long m;
                cin>>m;
                sum++;
                y[sum]=m;

            }
            else if(a=="pop")
            {
                if(sum==0)
                    {
                        cout<<"Empty"<<endl;
                    }
                    else{
                        sum--;

                    }
            }
            else if(a=="query")
            {
                if(sum==0) cout<<"Anguei!"<<endl;
                else cout<<y[sum]<<endl;
            }
            else if(a=="size")
            {
                cout<<sum<<endl;
            }
        }

    }
    return 0;
}

by shx2011 @ 2024-10-20 22:47:58

@quminnan 如果不介意的话可以试试STL,没必要手搓 但是如果你想手搓了解原理当我没说(无恶)


by shx2011 @ 2024-10-20 22:51:10

@shx2011 或者STL可能会TLE也当我没说


by _qumingnan_ @ 2024-10-21 19:03:26

谢谢各位,我把栈头当栈底了


by _qumingnan_ @ 2024-10-21 19:10:00

@wangyihan_ 栈要清空


by zsq9 @ 2024-10-21 19:11:43

@wangyihan_ 就是这个

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

by zsq9 @ 2024-10-21 19:13:06

@wangyihan_
变量名错了是这个

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

|