문제 설명
정수 리스트 num_list가 주어집니다. 가장 첫 번째 원소를 1번 원소라고 할 때, 홀수 번째 원소들의 합과 짝수 번째 원소들의 합 중 큰 값을 return 하도록 solution 함수를 완성해주세요. 두 값이 같을 경우 그 값을 return합니다.
using System;
public class Solution {
public int solution(int[] num_list) {
int answer = 0;
int a = 0;
int b = 0;
for(int i = 0; i< num_list.Length; i += 2)
{
a += num_list[i];
}
for(int i = 1; i< num_list.Length; i += 2)
{
b += num_list[i];
}
answer = a > b ? a : b;
return answer;
}
}
다른 풀이
using System;
public class Solution {
public int solution(int[] num_list) {
int even = 0;
for(int i = 0; i < num_list.Length; i += 2)
even += num_list[i];
int odd = 0;
for(int i = 1; i < num_list.Length; i += 2)
odd += num_list[i];
return Math.Max(even, odd);
}
}