본문 바로가기
Algorithm

[BAEKJOON] 10828번: 스택

by Y06 2021. 9. 30.

10828번 문제이다.

사진을 누르면 문제로 이동한다.

문제는 C++언어로 구현하였다.

import sys
input=sys.stdin.readline

n = int(input()) #명령의 수
stack=[]

def push(x):
    #리스트의 마지막에 추가
    stack.append(x) #딱히 int(x)할 필요는 없을 듯

def pop():
    if(len(stack)==0):
        print(-1)
    else:
        print(stack.pop())

def size():
    print(len(stack))

def empty():
    if(len(stack)==0): #비면
        print(1)
    else: #안 비면
        print(0)

def top():
    if(len(stack)==0):
        print(-1)
    else:
        print(stack[-1])


for i in range(n):
    command=input().split() #push 1 같은 애들 분리
    if(command[0] == 'push'):
        push(command[1])
    if (command[0] == 'pop'):
        pop()
    if (command[0] == 'size'):
        size()
    if (command[0] == 'empty'):
        empty()
    if (command[0] == 'top'):
        top()

'Algorithm' 카테고리의 다른 글

[BAEKJOON] 2178번: 미로탐색  (0) 2021.10.26
[BAEKJOON] 1260번: DFS와 BFS  (0) 2021.10.26
[BAEKJOON] 1874번: 스택 수열  (0) 2021.09.30
[BAEKJOON] 1074번: Z  (0) 2021.09.27
[BAEKJOON] 2018번: 수들의 합 5  (0) 2021.09.26