Skip to content

Commit 5ae789c

Browse files
committed
[BOJ] #28278.스택2 / 실버4 / 30분(∆)
1 parent 4e0d073 commit 5ae789c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
input = sys.stdin.read # 한 번에 모든 입력을 읽어옴
3+
output = sys.stdout.write
4+
5+
def stack_operations(N, data):
6+
stack = []
7+
results = []
8+
9+
for i in range(1, N+1):
10+
command = data[i].split() # 명령어를 공백을 기준으로 나눠서 리스트로 저장
11+
12+
if command[0] == '1':
13+
X = int(command[1])
14+
stack.append(X)
15+
elif command[0] == '2':
16+
if stack:
17+
results.append(str(stack.pop()))
18+
else:
19+
results.append('-1')
20+
elif command[0] == '3':
21+
results.append(str(len(stack)))
22+
elif command[0] == '4':
23+
results.append('1' if not stack else '0')
24+
elif command[0] == '5':
25+
if stack:
26+
results.append(str(stack[-1]))
27+
else:
28+
results.append('-1')
29+
30+
output("\n".join(results) + "\n")
31+
32+
data = input().splitlines() # 입력을 줄 단위로 나눠서 리스트로 저장
33+
N = int(data[0])
34+
stack_operations(N, data)

0 commit comments

Comments
 (0)