반응형
https://www.acmicpc.net/problem/10828
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder output = new StringBuilder();
static StringTokenizer tokens;
static int N, size;
static int[] stack;
public static void main(String[] args) throws NumberFormatException, IOException {
N =Integer.parseInt(input.readLine());
stack = new int[N];
for (int n = 0; n < N; n++) {
tokens = new StringTokenizer(input.readLine());
switch(tokens.nextToken()) {
case "top":
output.append(top()).append("\n");
break;
case "pop":
output.append(pop()).append("\n");
break;
case "size":
output.append(size()).append("\n");
break;
case "empty":
output.append(empty()).append("\n");
break;
case "push":
push(Integer.parseInt(tokens.nextToken()));
break;
}
}
System.out.println(output);
}
static void push(int n) {
stack[size++] = n;
}
static int top() {
if(size==0)return -1;
return stack[size-1];
}
static int pop() {
if(size==0)return -1;
int tmp = stack[--size];
stack[size] = 0;
return tmp;
}
static int size() {
return size;
}
static int empty() {
if(size == 0)return 1;
else return 0;
}
}
반응형
'코딩테스트 > 백준문제' 카테고리의 다른 글
[Java] 백준_3059_등장하지않는문자의합 (0) | 2023.01.26 |
---|---|
[Java] 백준_16928_뱀과 사다리 게임 (0) | 2021.07.10 |
[Java] 백준_10815_숫자카드 (0) | 2021.05.24 |
[Java] 백준_2750_수 정렬하기 (0) | 2021.05.22 |
[Java] 백준_1920_수찾기 (0) | 2021.05.21 |