3138 栈练习2

3138 栈练习2

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

(此题与栈练习1相比改了2处:1加强了数据 2不保证栈空时不会出栈)

给定一个栈(初始为空,元素类型为整数,且小于等于100),只有两个操作:入栈和出栈。先给出这些操作,请输出最终栈的栈顶元素。  操作解释:1表示入栈,2表示出栈

输入描述 Input Description

N(操作个数)

N个操作(如果是入栈则后面还会有一个入栈元素)

具体见样例(输入不保证栈空时不会出栈)

输出描述 Output Description

最终栈顶元素,若最终栈空,或栈空时有出栈操作,输出”impossible!”(不含引号)

样例输入 Sample Input

3

1 2

2

2

样例输出 Sample Output

impossible!

数据范围及提示 Data Size & Hint

对于100%的数据  N≤100000 元素均为正整数且小于等于10^8

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
 long long n,top,m,k;
 cin>>n;
 top=0;
 for(int i=1;i<=n;++i)
 {
  cin>>m;
  if(m==1)
  {
   cin>>k;
   s.push(k);
   top+=1;
  }
  else
  {
   if(top==0)
   {
    cout<<"impossible!";
    return 0;
   }
   s.pop();
   top-=1;
  }
 }
 if(top==0)
   cout<<"impossible!";
 else
   cout<<s.top();
 return 0;
}
原文地址:https://www.cnblogs.com/lyqlyq/p/6628928.html