zhouchenrui @ 2024-11-25 20:22:56
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
stack <int> s;
signed main(){
int t;
cin>>t;
for(int i=1;i<=t;i++){
int n;
cin>>n;
while(n--){
string a;
cin>>a;
if(a=="push"){
int g;
cin>>g;
s.push(g);
}else if(a=="pop"){
if(!s.empty()){
s.pop();
}else{
cout<<"Empty"<<endl;
}
}else if(a=="query"){
if(!s.empty()){
cout<<s.top()<<endl;
}else{
cout<<"Anguei!"<<endl;
}
}else{
cout<<s.size()<<endl;
}
}
}
return 0;
}
by xiaoke2021 @ 2024-11-25 20:28:32
@zhouchenrui 多测不清空,保龄两行泪
by xiaoke2021 @ 2024-11-25 20:28:51
能给个关吗
by zhouchenrui @ 2024-11-25 20:30:07
@xiaoke2021 怎么清 已关
by ToMaT @ 2024-11-25 20:31:30
@zhouchenrui
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
signed main(){
int t;
cin>>t;
for(int i=1;i<=t;i++){
int n;
cin>>n;
stack<int> s;
while(n--){
string a;
cin>>a;
if(a=="push"){
int g;
cin>>g;
s.push(g);
}else if(a=="pop"){
if(!s.empty()){
s.pop();
}else{
cout<<"Empty"<<endl;
}
}else if(a=="query"){
if(!s.empty()){
cout<<s.top()<<endl;
}else{
cout<<"Anguei!"<<endl;
}
}else{
cout<<s.size()<<endl;
}
}
}
return 0;
}
by xiaoke2021 @ 2024-11-25 20:31:39
@zhouchenrui
while(!s.empty()) s.pop();
放在多测循环中。
by ToMaT @ 2024-11-25 20:32:26
@zhouchenrui 把栈定义到里面
by ToMaT @ 2024-11-25 20:33:35
@xiaoke2021 直接在循环里面定义栈复杂度会不会更好
by xiaoke2021 @ 2024-11-25 20:34:33
@ToMaT理论来讲是的,但有些时候可能会玄学 re。这种情况我见过。
by zhouchenrui @ 2024-11-25 20:36:14
谢谢~