728x90
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
728x90
n = int(input())
stack = []
ans = []
count = 0
for i in range(0, n):
s = input().split()
if s[0] == 'push':
stack.append(s[1])
elif s[0] == 'pop':
if len(stack) == 0:
ans.append(-1)
else:
ans.append(stack[len(stack)-1])
del stack[len(stack)-1]
elif s[0] == 'size':
ans.append(len(stack))
elif s[0] == 'empty':
if len(stack) == 0:
ans.append(1)
else:
ans.append(0)
elif s[0] == 'top':
if len(stack) == 0:
ans.append(-1)
else:
ans.append(stack[len(stack)-1])
for i in range(0, len(ans)):
print(ans[i])
728x90
'Problem Solving > BOJ' 카테고리의 다른 글
[백준, BOJ 2920] 음계 (python) (0) | 2021.05.10 |
---|---|
[백준, BOJ 2750] 수 정렬하기 (python) (0) | 2021.05.10 |
[백준, BOJ 9012] 괄호 (python) (0) | 2021.05.09 |
[백준, BOJ 10946] 랜덤 게임~~~~ (python) (0) | 2021.05.09 |
[백준, BOJ 2798] 블랙잭 (python) (0) | 2021.05.09 |