Coding Test

n의 배수 고르기

honey-vision 2024. 6. 28. 17:33

문제 설명
정수 n과 정수 배열 numlist가 매개변수로 주어질 때, numlist에서 n의 배수가 아닌 수들을 제거한 배열을 return하도록 solution 함수를 완성해주세요.

 

#include <stdio.h>
#include <stdlib.h>

int* solution(int n, int numlist[], size_t numlist_len) {
    int* answer = (int*)malloc(numlist_len * sizeof(int));
    size_t count = 0;

    for (size_t i = 0; i < numlist_len; i++) {
        if (numlist[i] % n == 0) {
            answer[count++] = numlist[i];
        }
    }

    return realloc(answer, count * sizeof(int));
}

 

'Coding Test' 카테고리의 다른 글

배열 자르기  (0) 2024.06.30
배열 두 배 만들기  (0) 2024.06.29
가장 큰 수 찾기  (0) 2024.06.27
대문자와 소문자  (0) 2024.06.25
암호 해독  (0) 2024.06.24