코딩테스트준비
백준 10828 자바
예쁜꽃이피었으면
2020. 9. 19. 02:47
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 �
www.acmicpc.net
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]);
}
}
}
}
}
반응형