[공부 내용]
백준 28278, 스택 2 문제
https://www.acmicpc.net/problem/28278
<문제>
스택 구현 후 명령 처리
명령의 수 N과 N개의 명령이 입력으로 들어온다.
<python 코드>
import sys
def commanding(inp, st, value=None):
if inp == 1:
st.append(value)
elif inp == 2:
if len(st)==0:
print(-1)
else:
print(st.pop())
elif inp == 3:
print(len(st))
elif inp == 4:
if len(st)==0:
print(1)
else:
print(0)
elif inp == 5:
if len(st)==0:
print(-1)
else:
print(st[-1])
n = int(input())
lst = []
for _ in range(n):
inpu = sys.stdin.readline().split() # 공백 기준으로 나눔
cmd = int(inpu[0]) # 명령어 번호
if cmd == 1:
val = int(inpu[1]) # 스택에 넣을 숫자
commanding(1, lst, val)
else:
commanding(cmd, lst)
input을 input.split()으로 받으니 시간초과가 뜨기에 sys를 import하여 sys.stdin.readline().split()을 사용하였다.
'스터디 > 2024 동계 모각코 개인' 카테고리의 다른 글
[2024 동계 모각코] 5회차 (2025/02/04) (1) | 2025.02.04 |
---|---|
[2024 동계 모각코] 4회차 (2025/01/28) (2) | 2025.01.28 |
[2024 동계 모각코] 3회차 (2025/01/21) (0) | 2025.01.21 |
[2024 동계 모각코] 2회차 (2025/01/14) (0) | 2025.01.14 |
2024 동계 모각코 계획 (1) | 2025.01.04 |