Baekjoon algorithm training

[백준] 10828 (스택)

interfacer_han 2023. 11. 25. 14:05

#1 알고리즘

java.util 패키지의 Stack 클래스를 이용한다.

 

#2 코드

#2-2 자바

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
        Stack<Integer> stack = new Stack<Integer>();
        
        int commandCount = Integer.parseInt(br.readLine().trim());
        
        for(int i = 0; i < commandCount; i++) {
            String command = br.readLine().trim();
            
            if(command.contains("push")) {
                int pushValue = Integer.parseInt(command.substring(5)); // 6번째 문자부터 끝 문자까지 추출
                stack.push(pushValue);
                
            } else if(command.equals("pop")) {
                if(stack.isEmpty()) {
                	bw.write("-1\n");
                } else {
                	bw.write(Integer.toString(stack.pop()) + "\n");
                }
                
            } else if(command.equals("size")) {
                if(stack.isEmpty()) {
                	bw.write("0\n");
                } else {
                	bw.write(Integer.toString(stack.size()) + "\n");
                }
                
            } else if(command.equals("empty")) {
                if(stack.isEmpty()) {
                	bw.write("1\n");
                } else {
                	bw.write("0\n");
                }
                
            } else if(command.equals("top")) {
                if(stack.isEmpty()) {
                	bw.write("-1\n");
                } else {
                	bw.write(Integer.toString(stack.peek()) + "\n");
                }
                
            } else {
                continue;
            }
        }
        
        br.close();
        
        bw.flush();
        bw.close();
    }
}

 

#2-2 코틀린

import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.util.Stack

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    
    val stack = Stack<Int>()

    val commandCount = br.readLine().trim().toInt()

    for(i : Int in 1..commandCount) {
        val command = br.readLine().trim()

        if(command.contains("push")) {
            val pushValue : Int = command.substring(5).toInt() // 6번째 문자부터 끝 문자까지 추출
            stack.push(pushValue)

        } else if(command == "pop") {
            if(stack.isEmpty()) {
                bw.write("-1\n")
            } else {
                bw.write("${stack.pop()}\n")
            }

        } else if(command == "size") {
            if(stack.isEmpty()) {
                bw.write("0\n")
            } else {
                bw.write("${stack.size}\n")
            }

        } else if(command == "empty") {
            if(stack.isEmpty()) {
                bw.write("1\n")
            } else {
                bw.write("0\n")
            }

        } else if(command == "top") {
            if(stack.isEmpty()) {
                bw.write("-1\n")
            } else {
                bw.write("${stack.peek()}\n")
            }

        } else {
            continue
        }
    }
    
    br.close()
    
    bw.flush()
    bw.close()
}

'Baekjoon algorithm training' 카테고리의 다른 글

[백준] 11651 (좌표 정렬하기 2)  (0) 2023.11.28
[백준] 8958 (OX퀴즈)  (0) 2023.11.27
[백준] 10818 (최소, 최대)  (0) 2023.11.24
[백준] 10250 (ACM 호텔)  (0) 2023.11.23
[백준] 2920 (음계)  (0) 2023.11.22