1. 문제

2. 코드
import sys
N = int(sys.stdin.readline())
stack = []
for _ in range (N):
word = sys.stdin.readline().split()
order = word[0]
if order == 'push':
stack.append(int(word[1]))
# word로 받은 정수를 stack에 추가
elif order == 'top':
if len(stack) >0:
print(stack[-1])
else:
print(-1)
#stack에 원소가 있으면 가장 마지막 원소 print
elif order == 'pop':
if len(stack)>0:
print(stack.pop())
else:
print(-1)
#stack에 원소가 있으면 가장 마지막 원소 pop
elif order == 'size':
print(len(stack))
#stack의 길이 print
elif order == 'empty':
if len(stack)>0:
print(0)
else:
print(1)
#stack에 원소있으면 0, 없으면 1 print
else:
break반응형
