[백준] 10845 큐

파이톨치·2022년 8월 8일
0

백준

목록 보기
4/12

코드

import sys

# 명령어 개수 
n = int(input())

# 함수들
def functions(queue, cmd):
    cmd = cmd.split()
    command = cmd[0]
    l_size = len(queue)

    if command == 'push':
        queue.append(int(cmd[1]))
    elif command == 'pop':
        if l_size == 0: print(-1)
        else: print(queue.pop(0))
    elif command == 'size':
        print(l_size)
    elif command == 'empty':
        if l_size == 0: print(1)
        else: print(0)
    elif command == 'front':
        if l_size != 0: print(queue[0])
        else: print(-1)
    elif command == 'back':
        if l_size != 0: print(queue[-1])
        else: print(-1)

# 입력 받기
queue = []
for i in range(n):
    com = sys.stdin.readline()
    functions(queue, com)

이상한 점 : sys.stdin.readline()를 쓰지 않으면 시간 초과로 풀리지 않는다. 왜 이렇게 만들어 놓았는지 솔직히 잘 모르겠다.

문제 링크

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

profile
안알랴줌

0개의 댓글