[Programmers_CT] 40. 등차수열의 특정한 항만 더하기

문정준's avatar
Apr 07, 2025
[Programmers_CT] 40. 등차수열의 특정한 항만 더하기
 
💡
프로그래머스 코딩 테스트 38. 등차수열의 특정한 항만 더하기

1. 문제 설명

notion image
 

제한 사항

notion image

2. 입출력 예시

notion image

3. 코드 작성

class Solution { public int solution(int a, int d, boolean[] included) { int answer = 0; for(int i=0;i<included.length;i++) { if(included[i]) answer += (d * i + a); } return answer; } }
✏️

등차수열의 성질

  • for문의 index를 활용하여 included가 true일 때만 값을 더함
    • 등차수열의 n번째 항 S = d*n + a
    • if(included[i]) answer += (d*i + a);

4. 테스트 실행

notion image

5. 제출 후 채점

notion image
Share article

sxias