10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
import sys
num = int(sys.stdin.readline())
stack = []
for i in range(num):
order = sys.stdin.readline().strip()
if order.split()[0] == 'push':
stack.append(order.split()[1])
elif order == 'pop':
print(stack.pop() if stack else -1)
elif order == 'size':
print(len(stack))
elif order == 'empty':
print(0 if stack else 1)
elif order == 'top':
print(stack[-1] if stack else -1)
'코딩테스트 > 백준' 카테고리의 다른 글
백준 10866번 덱 python 파이썬 (0) | 2022.10.28 |
---|---|
백준 10845번 큐 python 파이썬 (0) | 2022.10.28 |
백준 11718번 그대로 출력하기 python 파이썬 (0) | 2022.10.07 |
백준 2941번 크로아티아 알파벳 python 파이썬 (0) | 2022.10.07 |
백준 9012번 괄호 python 파이썬 (0) | 2022.10.07 |