본문 바로가기
프로그래머스코딩테스트연습풀이/C언어

[프로그래머스/C언어] n의 배수 고르기

by 코코쵸마 2023. 8. 3.

문제

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

 

솔루션

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

// numlist_len은 배열 numlist의 길이입니다.
int* solution(int n, int numlist[], size_t numlist_len) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int* answer = (int*)malloc(numlist_len*sizeof(int));
    int j = 0;
    for(int i = 0; i < numlist_len; i++)
        if(numlist[i] % n == 0)
            answer[j++] = numlist[i];
    answer[j] = '\0';
    return answer;
}

https://school.programmers.co.kr/learn/courses/30/lessons/120905