본문 바로가기

프로그래머스/lv0

짝수 홀수 개수

/* 짝수 홀수 개수
 * 정수가 담긴 리스트 num_list가 주어질 때,
 * num_list의 원소 중 짝수와 홀수의 개수를 담은 배열을 return
 *
 * num_list         result
 * [1, 2, 3, 4, 5]  [2, 3]
 * [1, 3, 5, 7]     [0, 4]
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class programmer_0_52 {
    static int[] a1 = {12345};
    static int[] a2 = {1357};
    public int[] solution(int[] num_list) {
        int[] answer = {0,0};
        for(int i : num_list){
            if(i % 2 == 0)  answer[0]++;
            else            answer[1]++;
        }
        for(int i : answer) System.out.println(i);
        return answer;
    }
    public static void main(String args[]){
        programmer_0_52 t = new programmer_0_52();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

아이스 아메리카노  (0) 2022.12.05
배열 뒤집기  (0) 2022.12.05
배열 자르기  (0) 2022.12.05
이진수 더하기  (1) 2022.12.05
치킨 쿠폰  (0) 2022.12.05