방명록
- 백준(BAEKJOON) 10828 스택 - Kotlin2024년 07월 30일 18시 02분 42초에 업로드 된 글입니다.작성자: Moonsu99
https://www.acmicpc.net/problem/10828
풀이
import java.io.BufferedReader import java.io.InputStreamReader lateinit var stack: IntArray // 스택 저장 배열 var ptr = -1 // 스택의 현재 위치, -1로 현재 스택이 비어있다는 걸로 fun main() { val br = BufferedReader(InputStreamReader(System.`in`)) val testCase = br.readLine().toInt() stack = IntArray(testCase) for (i in 0..<testCase) { when (val input = br.readLine()) { "top" -> println(top()) "pop" -> println(pop()) "empty" -> println(isEmpty()) "size" -> println(size()) else -> push(input.split(" ")[1].toInt()) } } } fun push(x: Int) { stack[++ptr] = x // 입력받은 정수 스택에 추가하고 포인터 증가 } fun size(): Int { return ptr + 1 // 현재 스택에 있는 정수의 개수 반환 } fun isEmpty(): Int { return if (ptr == -1) 1 else 0 // 비어있으면 1 아니면 0 } fun pop(): Int { return if (ptr == -1) -1 else stack[ptr--] // 가장 위에 있는 정수 제거하고 반환, 비어있으면 -1 반환 } fun top(): Int { return if (ptr == -1) -1 else stack[ptr] // 가장 위에 있는 정수 반환 }
'코딩테스트 > 백준' 카테고리의 다른 글
[프로그래머스 LV2] 튜플 - Python (0) 2024.03.28 백준(BAEKJOON) 22942 데이터 체커 - Python (1) 2023.12.26 백준(BAEKJOON) 1918 후위표기식 - Python (1) 2023.12.26 백준(BAEKJOON) 2800 괄호제거 - Python (1) 2023.12.03 백준(BAEKJOON) 2504 괄호의 값 - Python (0) 2023.12.01 다음글이 없습니다.이전글이 없습니다.댓글