题解
2023-07-10 14:48:04
发布于:上海
200阅读
0回复
0点赞
#include <iostream>
#include <stack>
using namespace std;
int main()
{
	int n, x;
	cin >> n;
	string c;
	stack <int> s;
	for(int i = 1; i <= n; ++i)
	{
		cin >> c;
		if(c == "push")
		{
			cin >> x;
			s.push(x);
		}
		else if(c == "pop")
		{
			if(!s.empty())
			{
				cout << "pop " << s.top() << endl;
				s.pop();
			}
			else
			{
				cout << "pop fail" << endl;
			}
		}
		else if(c == "top")
		{
			if(!s.empty())
			{
				cout << "top = " << s.top() << endl;
			}
			else
			{
				cout << "top fail" << endl;
			}
		}
		else if(c == "size")
		{
			cout << "size = " << s.size() << endl;
		}
		else if(c == "empty")
		{
			if(!s.empty())
			{
				cout << "no" << endl;
			}
			else
			{
				cout << "yes" << endl;
			}
		}
	}
	return 0;
}
这里空空如也

有帮助,赞一个