https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.
www.acmicpc.net
import sys
stack_list = []
def main():
#while loop counting num.
n = int(input())
stack_pointer = -1
while n>0:
#input = sys.stdin.readline()
command = list(map(str,sys.stdin.readline().split()))
# print(command)
if 'push' in command:
stack_list.append(command[1])
stack_pointer = stack_pointer + 1
elif 'pop' in command:
if stack_pointer is -1:
print('-1')
else:
print(stack_list[stack_pointer])
del stack_list[stack_pointer]
stack_pointer = stack_pointer-1
elif 'size' in command:
print(stack_pointer+1)
elif 'empty' in command:
if stack_pointer is -1:
print(1)
else:
print(0)
elif 'top' in command:
if stack_pointer is -1:
print(-1)
else:
print(stack_list[stack_pointer])
n= n-1
main()
'OnlineJudge' 카테고리의 다른 글
Baek Joon Algorithm(백준 알고리즘): 10871번 (0) | 2019.08.27 |
---|---|
Baek Joon Algorithm(백준 알고리즘): 10869번 (0) | 2019.08.27 |
Baek Joon Algorithm(백준 알고리즘): 10430번 (0) | 2019.08.27 |
Baek Joon Algorithm(백준 알고리즘): 2884번 (0) | 2019.08.27 |
Baek Joon Algorithm(백준 알고리즘): 2753번 (0) | 2019.08.27 |