백준 10828번
제목 : 스택
난이도 실버 4
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(input.readLine());
ArrayList<Integer> stack = new ArrayList<Integer>();
String command = "";
for(int i =0;i<n; i++){
command = input.readLine();
if(command.contains("push")){
stack.add(Integer.parseInt(command.substring(5)));
}
else if(command.contains("pop")){
if(stack.size()==0)
{
System.out.println("-1");
}
else{
System.out.println(stack.get(stack.size()-1));
stack.remove(stack.size()-1);
}
}
else if(command.contains("size")){
System.out.println(stack.size());
}
else if(command.contains("empty")){
if(stack.isEmpty()==true){
System.out.println("1");
}
else{
System.out.println("0");
}
}
else{
if(stack.isEmpty()){
System.out.println("-1");
}
else{
System.out.println(stack.get(stack.size()-1));
}
}
}
}
}