一、题目
活动 - AcWing 系统讲解常用算法与数据结构,给出相应代码模板,并会布置、讲解相应的基础算法题目。https://www.acwing.com/problem/content/830/
二、题解
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int s[N], top;
void init()
{
top = -1;
}
void push(int x)
{
s[++top] = x;
}
void empty()
{
if (top == -1) cout << "YES\n";
else cout << "NO\n";
}
void pop()
{
top--;
}
void query()
{
cout << s[top] << "\n";
}
int main()
{
int m;
cin >> m;
init();
while (m -- ) {
string s;
cin >> s;
if (s == "query") query();
else if (s == "empty") empty();
else if (s == "pop") pop();
else {
int x;
cin >> x;
push(x);
}
}
return 0;
}