문제
두 정수 a, d와 길이가 n인 boolean 배열 included가 주어집니다. 첫째항이 a, 공차가 d인 등차수열에서 included[i]가 i + 1항을 의미할 때, 이 등차수열의 1항부터 n항까지 included가 true인 항들만 더한 값을 return 하는 solution 함수를 작성해 주세요.
솔루션
#include <string>
#include <vector>
using namespace std;
int solution(int a, int d, vector<bool> included) {
int answer = 0;
int n = a;
for(int i = 0; i < included.size(); i++){
if(included[i])
answer += n;
n += d;
}
return answer;
}
https://school.programmers.co.kr/learn/courses/30/lessons/181931
'프로그래머스코딩테스트연습풀이 > C++' 카테고리의 다른 글
[프로그래머스/C++] 문자열 섞기 (0) | 2025.03.06 |
---|---|
[프로그래머스/C++] 배열의 길이를 2의 거듭제곱으로 만들기 (0) | 2025.02.22 |
[프로그래머스/C++] 글자 지우기 (0) | 2025.02.22 |
[프로그래머스/C++] 수열과 구간 쿼리 1 (0) | 2025.02.22 |
[프로그래머스/C++] 합성수 찾기 (0) | 2025.02.22 |