백준 28278번: 스택 2 python

tomkitcount·2025년 3월 31일

매일 알고리즘

목록 보기
11/301

https://www.acmicpc.net/problem/28278

import sys

stack = []

n = int(sys.stdin.readline())


for _ in range(n):
    
    command = sys.stdin.readline().split()
    
    if command[0] == '1':
        stack.append(command[1])
    elif command[0] == '2':
        if stack:
            print(stack.pop())
        else:
            print(-1)
    elif command[0] == '3':
        print(len(stack))
    elif command[0] == '4':
        if stack:
            print(0)
        else:
            print(1)
    elif command[0] == '5':
        if stack:
            print(stack[-1])
        else:
            print(-1)

list에 append와 pop을 이용하여 스택을 구현할 수 있다.

profile
To make it count

0개의 댓글