https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
- 입력과 출력

- 소스코드
import sys
def push(lst , number):
lst.append(number)
return lst
def pop(lst):
if empty(lst) == 1:
return -1
popData = lst.pop()
return popData
def size(lst): return len(lst)
def empty(lst): return 1 if len(lst) == 0 else 0
def top(lst): return lst[-1] if empty(lst) == 0 else -1
res = list()
input = sys.stdin.readline
printLst = []
for _ in range(int(input())):
stack = list(input().split())
cmd = stack[0]
if cmd == 'push':
push(res,stack[1])
elif cmd == 'top':
printLst.append(top(res))
elif cmd == 'size':
printLst.append(size(res))
elif cmd == 'empty':
printLst.append(empty(res))
elif cmd == 'pop':
printLst.append(pop(res))
print('\n'.join(map(str,printLst)))
- 해설
스택을 구현 하는 문제이다 .
스택은 후입선출(LIFO, Last-In-First-Out) 구조 라 하며 쌓아 올린 형태의 자료 구조 로 가장 나중에 삽입된 데이터가 출력되거나 삽입 되는 구조
스택구조를 코딩하면 되는 문제인데 파이썬에서는 리스트 가 스택구조를 가지고있어 리스트로 간단하게 풀면된다
'Backjoon' 카테고리의 다른 글
| Backjoon problem 11866 요세푸스 문제 0 - python (0) | 2023.05.18 |
|---|---|
| Backjoon problem 1259 팰린드롬수 - python (0) | 2023.05.18 |
| Backjoon problem 1181 단어 정렬 - python (0) | 2023.05.18 |
| Backjoon problem 10898 수 정렬하기 3- python (0) | 2023.05.18 |
| Backjoon problem 1987 소수 찾기 - python (0) | 2023.05.04 |