栈是一种数据结构。现在你要支持几种操作:
-
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#include 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("%dn", Stack[top]); else //pop出栈 --top; } return 0; }



