why

B3614 【模板】栈

piyuhan666 @ 2024-09-12 17:46:04

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

by HotDogSeller @ 2024-09-12 17:50:14

@piyuhan666

你多测妹清空啊......


by piyuhan666 @ 2024-09-12 17:54:43

啥意思??? @HotDogSeller


by HotDogSeller @ 2024-09-12 17:56:47

@piyuhan666

这题一个测试点有多组数据,每次重新开始的时候你的栈应该是空的。不先清空栈就会残留上一轮的数据

举个例子:

2
1
push 1
1
size

by piyuhan666 @ 2024-09-12 17:58:55

ooo,互关 @HotDogSeller


by piyuhan666 @ 2024-09-12 18:03:33

33分 @HotDogSeller


by piyuhan666 @ 2024-09-12 18:09:32

@HotDogSeller用原始方法

#include<bits/stdc++.h>
using namespace std;
int t,n;
string za;
string x[1000005];
int top=0;
int main(){
    cin>>t;
    while(t--){
        top=0;
        cin>>n;
        while(n--){
            cin>>za;
            if(za=="push"){
                top++;
                cin>>x[top];                
            }
            else if(za=="pop"){
                if(top>0){
                    top--;
                }
                else{
                    cout<<"Empty"<<endl;
                }
            }
            else if(za=="size"){
                cout<<top<<endl;
            }
            else if(za=="query"){
                if(top>0){
                    cout<<x[top]<<endl;
                }
                else{
                    cout<<"Anguei!"<<endl;
                }
            }
        }
    }
    return 0;
}

|