본문 바로가기
백준알고리즘/스택

(Python/🥈4)백준 알고리즘 10828번:스택

by windy7271 2023. 5. 1.
728x90
반응형

문제출처:https://www.acmicpc.net/problem/10828

풀이:

import sys
sys.stdin = open('/Users/song/Desktop/Python/Python/h.txt', 'r')

N = int(input())

lst = []
res = []
for i in range(N):
    x = input().split(" ")
    if x[0] == "push":
        lst.append(x[1])
    elif x[0] =="top":
        if len(lst) == 0:
            res.append(-1)
        else:
            top = lst.pop()
            res.append(top)
            lst.append(top)
    elif x[0] == "size":
        now = len(lst)
        res.append(now)
    elif x[0] =="empty":
        if len(lst) == 0:
            res.append(1)
        else:
            res.append(0)
    elif x[0] == 'pop':
        if len(lst) == 0:
            res.append(-1)
        else:
            now = lst.pop()
            res.append(now)
print(*res,sep="\n")





 

 

단순 구현 문제

반응형

댓글