python

프로그래머스 : 등차수열의 특정한 항만 더하기 (파이썬)

유교주 2024. 4. 22. 13:16
728x90
반응형

요구사항

내답안

def solution(a, d, included):
    answer = 0
    for i in range(len(included)): #반복문을 돌려 included의 배열갯수만큼
        answer += (a + d * i) * int(included[i]) #cincluded[i] 가 int로 1, 0 참 거짓
        #으로 구분되는데 0을 곱하게되면 0이 결과가 되므로 합산에 더해지지않음.
        #print(int(included[i]))  1 0 0 1 1 / 0 0 0 1 0 0 0
    return answer

결과

후기

딱 문제해결을 하려할때 생각을 많이하게 되었고, print를 찍으면서 처리하니 은근 쉬웠던 문제였다.

728x90
반응형