문제 설명
두 정수 a, d와 길이가 n인 boolean 배열 included가 주어집니다. 첫째항이 a, 공차가 d인 등차수열에서 included[i]가 i + 1항을 의미할 때, 이 등차수열의 1항부터 n항까지 included가 true인 항들만 더한 값을 return 하는 solution 함수를 작성해 주세요.
using System;
public class Solution {
public int solution(int a, int d, bool[] included) {
int answer = 0;
int total = a;
for(int i = 0; i < included.Length; i++)
{
if(included[i])
{
answer += total;
}
total += d;
}
return answer;
}
}
다른 답변
using System;
public class Solution {
public int solution(int a, int d, bool[] included) {
int answer = 0;
for(int i=0;i<included.Length;i++)
{
if(included[i] == true)
answer += a + d * i;
}
return answer;
}
}
원소들의 곱과 합 (0) | 2024.06.10 |
---|---|
주사위 게임 2 (0) | 2024.06.10 |
코드 처리하기 (1) | 2024.06.10 |
flag에 따라 다른 값 반환하기 (0) | 2024.06.10 |
홀짝에 따라 다른 값 반환하기 (0) | 2024.06.10 |