코딩테스트/프로그래머스 Lv. 0

[프로그래머스] Lv. 0 등차수열의 특정한 항만 더하기 JAVA

촙오 개발자 2025. 2. 2. 22:21
반응형

등차수열의 특정한 항만 더하기

 

문제 설명

요구사항

  • 배열의 값이 true 일때만 등차 수열의 항을 더함

 

테스트

package lv0;

public class 등차수열의_특정한_항만_더하기 {
	public int solution(int a, int d, boolean[] included) {
        int answer = 0;
        
        for(int i = 0; i < included.length; i++) {
        	if(included[i]) {
        		answer += a + d * i;	// 등차수열의 규칙 n항 = 초기값 + (n-1) * 등차
        	}
        }
        
        return answer;
    }
}

 

프로그래머스

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 += a + d * i;
        	}
        }
        
        return answer;
    }
}

 

결과

반응형