栈是一种数据结构。现在你要支持几种操作:
-
push x
,将x这个元素放到栈顶。 -
pop
,将栈顶元素删除。 -
top
,询问栈顶元素是多少。
输入格式
第一行一个整数m,表示操作个数。
接下来m行,每行一个上面所述的操作。
输出格式
输出若干行,对于每个查询操作,输出答案。
样例输入
10
push 1
push 2
top
pop
top
push 3
top
pop
push 4
top
样例输出
2
1
3
4
数据规模
题解
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1e5+5;
int Stack[N], top = 0;
char tmp[5];
int main(){
int m;
scanf("%d", &m);
while(m--){
scanf("%s", tmp);
if(tmp[1] == 'u') //push入栈
scanf("%d", &Stack[++top]);
else if(tmp[0] == 't') //top返回栈顶
printf("%d\n", Stack[top]);
else //pop出栈
--top;
}
return 0;
}