알고리즘/알고리즘 문제풀기
백준 6단계 : 함수 (Python)
takudaddy
2021. 1. 5. 22:08
1. 정수 N개의 합 ( #15596번)
정수 n개가 주어졌을 때, n개의 합을 구하는 함수를 작성
[작성해야 하는 함수]
Python 2, Python 3, PyPy, PyPy3: def solve(a: list) -> int
a: 합을 구해야 하는 정수 n개가 저장되어 있는 리스트 (0 ≤ a[i] ≤ 1,000,000, 1 ≤ n ≤ 3,000,000)
리턴값: a에 포함되어 있는 정수 n개의 합 (정수)
def solve(a):
total = 0
for n in a:
total += n
return total
or
def solve(a):
return sum(a)
처리 시간이 위 코드가 5배 더 걸린다.
2. 셀프 넘버 ( #4673번)
def d(n):
a = 0
for x in list(str(n)):
a = a + int(x)
return int(n) + a
a = []
for i in range(1, 10001):
k = d(i)
a.append(k)
for b in range(1, 10001):
if b in a :
pass
else:
print(b)
728x90