728x90

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

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

import sys
input=sys.stdin.readline
n = int(input())
li = []

def push(value):
    li.append(value)

def top():
    print (li[-1] if len(li)>0 else -1)

def size():
    print(len(li))

def pop():
    print(li.pop() if len(li)>0 else -1)

def empty():
    print(1 if len(li)==0 else 0)

for i in range(n):
    order = input().split()
    if order[0] ==  'push':
        push(int(order[1]))
    elif order[0] == 'top':
        top()
    elif order[0] == 'size':
        size()
    elif order[0] == 'pop':
        pop()
    elif order[0] == 'empty':
        empty()

 

 

 

728x90

'Algorithm > 백준' 카테고리의 다른 글

[백준] 10845 큐  (0) 2023.07.06
[백준] 9012 괄호  (0) 2023.07.05
[백준] 10973 이전순열  (0) 2023.03.17
[백준] 10972 다음순열  (0) 2023.03.17
[백준] 156751 n과 m 3  (0) 2023.03.15

+ Recent posts