[백준 10828: 스택 ] python 파이썬

SUHA JEONG·2021년 11월 15일
0

Algorithm 문제

목록 보기
3/3


import sys
# 명령을 입력받을 변수 n
n = int(sys.stdin.readline())
# 스택(리스트) 선언
stack = []

# for문을 돌면서 명령을 입력받음
for _ in range(n):
   word = sys.stdin.readline().split()
   order = word[0]

   if order == "push":
      value = word[1]
      stack.append(value)
  
   elif order == "pop":
      if len(stack) == 0:
         print(-1)
      else:
         print(stack.pop())
  
   elif order == "size":
      print(len(stack))
  
   elif order == "empty":
      if len(stack) == 0:
         print(1)
      else:
         print(0)

   elif order == "top":
      if len(stack) == 0:
         print(-1)
      else:
         print(stack[-1])

0개의 댓글