프로그래머스/Lv.0

[프로그래머스 C#] 길이에 따른 연산

suniverse 2023. 8. 5. 12:53

 

using System;

public class Solution {
    public int solution(int[] num_list) {
        int answer = 0;
        
        int mul = 1;
        
        for(int i = 0; i < num_list.Length; i++)
        {
            if(num_list.Length >= 11)
            {
                answer += num_list[i];
            }
            else if(num_list.Length <= 10)
            {
                mul *= num_list[i];
                answer = mul;
            }
        }
        
        return answer;
    }
}

 

✍ 곱셈의 경우 초기값이 0이면 곱셈이 되지 않기 때문에 1로 초기화한 변수 mul을 선언하여 사용하였다.