본문 바로가기
(문제풀이)

프로그래머스(java) : n의 배수 구하기

by cogito-new 2022. 10. 11.

class Solution {
    public int[] solution(int n, int[] numlist) {
        
        int[] arry = new int[numlist.length];
        int count = 0;
        for(int i = 0; i<numlist.length; i++){
            if( numlist[i]%n == 0){
                arry[count] = numlist[i];
                count++;
            }
        }
        
        int[] answer = new int[count];
        for(int i = 0; i<count; i++){
            answer[i] = arry[i];
        }
        
        return answer;
    }
}

n의 배수 구하기 : 배열 값마다 입력받은 값 n 으로 나누었을 때, 나머지의 유무에 따라 배수를 판단.

반응형