반응형
https://www.acmicpc.net/problem/10773
10773번: 제로
첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경
www.acmicpc.net
class stack:
def __init__(self):
self.tmp = []
def push(self,x):
self.tmp.append(x)
def pop(self):
if self.size() == 0:
return -1
else:
return self.tmp.pop(-1)
def size(self):
return len(self.tmp)
def empty(self):
if self.size() == 0:
return 1
else:
return 0
def top(self):
if self.size()==0:
return -1
else:
return self.tmp[-1]
def hap(self):
return sum(self.tmp)
import sys
N = int(sys.stdin.readline())
s = stack()
for _ in range(N):
x = int(sys.stdin.readline())
if x != 0:
s.push(x)
else:
s.pop()
print(s.hap())
일부러 모든 스택의 함수를 구현하였다(연습용) 필요시 삭제해도 무관하다.
반응형
'백준 문제풀이' 카테고리의 다른 글
python - baekjoon - 10814 (0) | 2020.09.05 |
---|---|
baekjoon - python - 1181 (0) | 2020.09.05 |
baekjoon - python - 10952 (0) | 2020.09.05 |
baekjoon - python - 10951 (0) | 2020.09.05 |
baekjoon - python - 10950 (0) | 2020.09.05 |
댓글