티스토리 뷰

문제


코드

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


'알고리즘 > 백준알고리즘(JAVA)' 카테고리의 다른 글

#1152번 : 단어의 개수  (0) 2018.09.03
#1237번 : 정ㅋ벅ㅋ  (0) 2018.08.25
#1001번 : A-B  (0) 2018.08.01
#1000번 : A+B  (0) 2018.07.31
#2557번 : Hello World  (0) 2018.07.31
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함