[프로그래머스/Java] Lv.0 원소들의 곱과 합

febCho·2024년 3월 2일
0

코딩테스트

목록 보기
24/253
post-thumbnail

문제

정수가 담긴 리스트 num_list가 주어질 때, 모든 원소들의 곱이 모든 원소들의 합의 제곱보다 작으면 1을 크면 0을 return하도록 solution 함수를 완성해주세요.

- 제한사항

  • 2 ≤ num_list의 길이 ≤ 10
  • 1 ≤ num_list의 원소 ≤ 9

풀이

관건은 평소 아무렇지 않게 0으로 초기화하는 정수형 변수를 int multi = 1과 같이 1로 초기화하는 것이라고 생각한다.

class Solution {
    public int solution(int[] num_list) {
        int multi = 1;
        int sum = 0;
        
        for(int i=0;i<num_list.length;i++){
            multi *= num_list[i];
            sum += num_list[i];
        }
        
        return multi > sum * sum ? 0 : 1;
    }
}

결과

결과

profile
Done is better than perfect.

0개의 댓글