蒟蒻の第一次栈的尝试以失败告终//全RE零分求调

B3614 【模板】栈

yoyoSGH @ 2024-09-25 16:54:31

PS:样例好像是对的

#include<bits/stdc++.h>
using namespace std;
const int MAX=100000;
//定义栈 
typedef struct Stack
{
    int data[MAX],top;
}Stact;
//初始化 
void doempty(Stack &S)
{
    S.top=-1;
}
//判空 
bool isitempty(Stack S)
{
    if(S.top==-1) return true;
    else return false;
}
//判高 
int size(Stack S)
{
    return (S.top)+1;
}
//进栈
void push(Stack &S,int thing)
{
    if(S.top==MAX-1)
    {
        cout<<"Sorry,it is full~"<<endl;
    }
    else
    {
        S.data[S.top+1]=thing;
        S.top++;
    }
}
//出栈
int pop(Stack &S,int thing)
{
    if(S.top==-1)
    {
        cout<<"Empty"<<endl;
    }
    else
    {
        thing=S.data[S.top];
        S.top--;
    }
}
//读取栈顶元素
void query(Stack S)
{
    if(S.top==-1) cout<<"Anguei!"<<endl;
    else cout<<S.data[S.top]<<endl;
} 
int main() {
    Stack S;
    doempty(S);
    int num;
    cin>>num;
    for(int i=0;i<num;i++)
    {
        int n;
        cin>>n;
        for(int j=0;j<n;j++)
        {
            string s;
            cin>>s;
            if(s=="push")
            {
                int x;
                cin>>x;
                push(S,x);
            }
            else if(s=="query")
            {
                query(S);
            }
            else if(s=="size")
            {
                cout<<size(S)<<endl;
            }
            else
            {
                int x;
                pop(S,x);
                cout<<x<<endl;
            }
        }
        doempty(S);
    }
    return 0;
}

by DesignDigits @ 2024-09-25 16:57:57

虽然但是,n\leq 10^6()


by DesignDigits @ 2024-09-25 16:58:54

可以写成 const int MAX=1e6+5;


by yoyoSGH @ 2024-09-25 17:08:08

@Mechanical_Melodies Thankyou已关


|