정수 n과 정수 배열 numlist가 매개변수로 주어질때, numlist에서 n의 배수가 아닌 수들을 제거한 배열을 return하도록 solution 함수를 완성하기
| n | numlist | result |
|---|---|---|
| 3 | [4, 5, 6, 7, 8, 9, 10, 11, 12] | [6, 9, 12] |
| 5 | [1, 9, 3, 10, 13, 5] | [10, 5] |
| 12 | [2, 100, 120, 600, 12, 12] | [120, 600, 12, 12] |
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int n, vector<int> numlist) {
vector<int> answer;
for (int i = 0; i < numlist.size(); i++){
if (numlist[i]%n == 0) {
answer.push_back(numlist[i]);
}
}
return answer;
}
#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(sizeof(int) * numlist_len);
int idx = 0;
for (int i=0; i<numlist_len; i++)
{
if (numlist[i] % n == 0)
{
answer[idx] = numlist[i];
idx++;
}
}
return answer;
}
열심히 하면 점점 더 좋아질거예요! o((>ω< ))o