CODING TEST/백준
백준 알고리즘 | 15596번 (정수 N개의 합) | 파이썬
sustronaut
2021. 7. 10. 17:45
15596번 (정수 N개의 합) *
- 정수 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 i in a:
total = total + i
int(total)
return total
의아한 점
def solve(a):
n = len(a)
total = 0
print(n)
# 홀수 일때
if(n % 2 == 1):
total = (a[0]+a[n-1])*(n/2)+n/2+1
int(total)
# 짝수 일때
else:
total = (a[0]+a[n-1])*(n/2)
int(total)
return total
- 왜 이 코드는 런타임 에러가 뜨는지 모르겠다..