题解
2023-07-30 21:18:15
发布于:广东
16阅读
0回复
0点赞
不多说,上题解
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(){
	int n;
	stack<int> a;
	string str;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>str;
		if(str=="empty"){
			if(a.empty()){
				cout<<"yes";
			}
			else{
				cout<<"no";
			}
			cout<<endl;
		}
		if(str=="push"){
			int x;
			cin>>x;
			a.push(x);
		}
		if(str=="top"){
			if(a.empty()) cout<<"top fail";
			else
				cout<<"top = "<<a.top();
			cout<<endl;
		}
		if(str=="size"){
			cout<<"size = "<<a.size();
			cout<<endl;
		}
		if(str=="pop"){
			if(a.empty()) cout<<"pop fail";
			else{
				cout<<"pop "<<a.top();
				a.pop();
			}
			cout<<endl;
		}
	}
	return 0;
}
这里空空如也

有帮助,赞一个