문제 설명
두 정수 a, d와 길이가 n인 boolean 배열 included가 주어집니다. 첫째항이 a, 공차가 d인 등차수열에서 included[i]가 i + 1항을 의미할 때, 이 등차수열의 1항부터 n항까지 included가 true인 항들만 더한 값을 return 하는 solution 함수를 작성해 주세요.
제한사항
- 1 ≤ a ≤ 100
- 1 ≤ d ≤ 100
- 1 ≤ included의 길이 ≤ 100
- included에는 true가 적어도 하나 존재합니다.
입출력 예

코드
class Solution {
public int solution(int a, int d, boolean[] included) {
int answer = 0;
int in = 0;
int sum = a;
for(int i = 0; i < included.length; i++){
if(included[i]){ //true
answer+=a;
}
a+=d;
}
return answer;
}
}
문제 출처
https://school.programmers.co.kr/learn/courses/30/lessons/181931
'Study > Test(Java)' 카테고리의 다른 글
| [프로그래머스] 옷가게 할인 받기 (0) | 2023.05.18 |
|---|---|
| [프로그래머스] 이어 붙인 수 Java (0) | 2023.05.18 |
| [프로그래머스] 코드 처리하기 Java (0) | 2023.05.18 |
| [백준] 9012 괄호 Java (0) | 2023.05.18 |
| [백준] 9093 단어 뒤집기 Java (0) | 2023.05.17 |