不会的看我!!!
2024-02-17 10:12:27
发布于:广东
28阅读
0回复
0点赞
#include <bits/stdc++.h>
#include <stack>
using namespace std;
stack<int>st;
int main(){
	int n;
	string s;
	cin>>n;
	while(n--){
		cin>>s;
		if(s=="push"){
			int k;
			cin>>k;
			st.push(k);
		}
		else if(s=="top"){
			if(st.empty()==true) cout<<"top fail\n";
			else cout<<"top = "<<st.top()<<endl;
		}
		else if(s=="size"){
			cout<<"size = "<<st.top()<<endl;
		}
		else if(s=="empty"){
			if(st.empty()==true) cout<<"yes\n";
			else cout<<"no\n";
		}
		else if(s=="pop"){
			if(st.empty()==true) cout<<"pop fail\n";
			else{
				cout<<"pop "<<st.top()<<endl;
				st.pop(); 
			}
		}
	}
	
    return 0;
}
这里空空如也

有帮助,赞一个