문제

코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
int stackTop = -1;
String input;
for (int i = 0; i < arr.length; i++) {
input = scan.next();
if (input.equals("push")) {
// 정수 X를 스택에 넣는 연산이다.
stackTop++;
arr[stackTop] = scan.nextInt();
} else if (input.equals("pop")) {
// 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다.
// 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
if (stackTop == -1) {
System.out.println(-1);
} else {
System.out.println(arr[stackTop]);
stackTop--;
}
} else if (input.equals("size")) {
// 스택에 들어있는 정수의 개수를 출력한다.
System.out.println(stackTop + 1);
} else if (input.equals("empty")) {
// 스택이 비어있으면 1, 아니면 0을 출력한다.
if (stackTop == -1) {
System.out.println(1);
} else {
System.out.println(0);
}
} else if (input.equals("top")) {
// 스택의 가장 위에 있는 정수를 출력한다.
// 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
if (stackTop == -1) {
System.out.println(-1);
} else {
System.out.println(arr[stackTop]);
}
}
}
}
}
출처
https://www.acmicpc.net/problem/10828