9 solutions

  • 0
    @ 2022-1-2 10:27:42
    #include <iostream>
    #include <cstring>
    #include <stack>
    using namespace std;
    int main()
    {
    	
    	int t,n,b;
    	cin>>t;
    	while(t--)
    	{
    	cin>>n;
    	stack<int>a;
    	while(n--)
    	{
    		char s[20];
    		cin>>s;
    		if(!strcmp(s,"pop"))//将字符串匹配,符合就进行下列操作
    		{
    			if(a.empty()) cout<<"Empty"<<endl;//先判断它是否为空,空就按题目要求输出
    			else 
    			{
    				cout<<a.top()<<endl;//否则就输出出栈元素
    				a.pop();//同时删除
    			} 
    		}
    		else if(!strcmp(s,"push"))//匹配就直接入栈
    		{
    			cin>>b;
    			a.push(b);
    		}	
    	}
    	}
    	return 0;
    }
    

    Information

    ID
    330
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    2
    Tags
    # Submissions
    152
    Accepted
    48
    Uploaded By