题解
2023-08-24 14:03:00
发布于:广东
6阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int main(){
	stack<int> a;
	int n;
	cin>>n;
	string commands[101]; // 暂存指令
	for(int i=0;i<=n;i++)
	{
		string c;
		getline(cin, c);
		commands[i] = c;
	}
    // 前方高能
	for(int j=0;j<=n;j++)
	{
		string command = commands[j];
		if(command.find("push") != string::npos)
		{
			string tmp = command.substr(command.find(" ")+1);
			a.push(atoi(tmp.c_str()));
			continue;
		}
		else if(command.find("pop") != string::npos){
			if(a.size() == 0){
				cout<<"pop fail"<<endl;
				continue;
			}
			cout<<"pop "<<a.top()<<endl;
			a.pop();
			continue;
		}
		else if(command.find("top") != string::npos){
			if(a.size() == 0)
			{
				cout<<"top fail"<<endl;	
				continue;
			}
			cout<<"top = "<<a.top()<<endl;
			continue;
		}
		else if(command.find("size") != string::npos)
		{
			cout<<"size = "<<a.size()<<endl;
			continue;
		}
		else if(command.find("empty") != string::npos){
			if(a.size() == 0) cout<<"yes"<<endl;
			else cout<<"no"<<endl;
			continue;
		}
	}
}
这里空空如也


有帮助,赞一个