AcWing 828. 模拟栈
原题链接
简单
作者:
鼠鼠我
,
2021-01-29 12:47:51
,
所有人可见
,
阅读 220
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int N = 100010;
static int stk[];
static int top;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split("\\s+");
int m = Integer.parseInt(s[0]);
init();
while(m-- >0)
{
String ss[] = br.readLine().split("\\s+");
if(ss[0].equals("push"))
{
int x = Integer.parseInt(ss[1]);
push(x);
}
else if(ss[0].equals("pop"))
{
pop();
}
else if(ss[0].equals("empty"))
{
if(isEmpty())
System.out.println("YES");
else {
System.out.println("NO");
}
}
else if(ss[0].equals("query"))
{
int x = quary();
System.out.println(x);
}
}
}
static void init()//初始化
{
stk = new int[N];
top=0;
}
static void push(int x) {//加入x,top++后加x
stk[++top] = x;
}
static void pop()//弹出
{
top--;
}
static boolean isEmpty()
{
return top == 0?true:false;
}
static int quary()//返回当前top储存的值
{
return stk[top];
}
}