codevs 3139 栈练习3

3139 栈练习3

 提交地址:http://codevs.cn/problem/3139/

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

比起第一题,本题加了另外一个操作,访问栈顶元素(编号3,保证访问栈顶元素时或出栈时栈不为空),现在给出这N此操作,输出结果。

输入描述 Input Description

N

N次操作(1入栈 2出栈 3访问栈顶)

输出描述 Output Description

K行(K为输入中询问的个数)每次的结果

样例输入 Sample Input

6

1  7

3

2

1  9

1  7

3

样例输出 Sample Output

7

7

数据范围及提示 Data Size & Hint

 对于50%的数据 N≤1000 入栈元素≤200

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

 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 #include<stack>
 5 stack<int>sa;
 6 int main()
 7 {
 8     int n;
 9     scanf("%d",&n);
10     int tt,k;
11     for(int i=n;i>=1;i--)
12     {
13         scanf("%d",&tt);
14         if(tt==1)
15         {
16             scanf("%d",&k);
17             sa.push(k);
18         }
19         if(tt==2)
20         {
21             sa.pop();
22         }
23         if(tt==3)
24         {
25             printf("%d\n",sa.top());
26         }
27     }
28     return 0;
29 }

 

posted @ 2016-05-29 20:12  月沫  阅读(161)  评论(0编辑  收藏  举报