https://www.acmicpc.net/problem/10828
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class baekjoon_10828_01 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int TC = Integer.parseInt(br.readLine());
int[] stack = new int[TC];
int size = 0;
for(int tc = 0; tc < TC ; tc ++) {
String[] temp = br.readLine().split(" ");
String command = temp[0];
if(command.equals("push")) {
int data = Integer.parseInt(temp[1]);
stack[size] = data;
size += 1;
}else if(command.equals("pop")) {
if(size == 0) {
System.out.println("-1");
}else {
System.out.println(stack[size-1]);
size -= 1;
}
}else if(command.equals("size")) {
System.out.println(size);
}else if(command.equals("empty")) {
if(size == 0) {
System.out.println("1");
}else {
System.out.println("0");
}
}else if(command.equals("top")) {
if(size == 0) {
System.out.println("-1");
}else {
System.out.println(stack[size-1]);
}
}
}
}
}
반응형
'코딩테스트준비' 카테고리의 다른 글
[백준2750]수 정렬하기 (0) | 2024.01.25 |
---|---|
백준 9012 자바 (0) | 2020.09.19 |
백준 9093 자바 (0) | 2020.09.19 |
백준 11022 자바 (0) | 2020.09.10 |
백준 11021 자바 (0) | 2020.09.10 |