Notice
Recent Posts
Recent Comments
Link
마이라이프해피라이프
[알고리즘 스터디] 백준 10773번 Python 풀이 - list 자료형 다루기 본문
# 문제
https://www.acmicpc.net/problem/10773
10773번: 제로
첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경
www.acmicpc.net
# 접근
list에 넣고 빼는 것을 조건에 따라 반복
# 작성 코드
num = int(input())
num_list = []
sum = 0
for _ in range(num):
input_num = int(input())
if (input_num == 0):
del num_list[-1]
else:
num_list.append(input_num)
for list_item in num_list:
sum += list_item
print(sum)
# 기억해야 할 것
- list 자료형에서 가장 뒤에 자료를 추가할 때 list.append
- list 자료형에서 자료를 삽입할 때 list.insert(index, 삽입할 자료)
- list 자료형에서 자료를 제거할 때 del list[index]
- list 자료형에서 자료를 제거할 때 list.remove(자료)
+) count, extend, pop
'컴퓨터 > Python' 카테고리의 다른 글
[알고리즘 스터디] 백준 2798번 Python 풀이 - 블랙잭 (0) | 2022.06.03 |
---|---|
[알고리즘 스터디] 백준 1259번 Python 풀이 - 팰린드롬수 (0) | 2022.05.02 |
[알고리즘 스터디] 백준 9012번 Python 풀이 - list() (0) | 2022.04.06 |
[python] List 초기화 방법 (0) | 2021.10.15 |
[python] list 자료형 - insert, remove (0) | 2021.10.01 |