프로그래머스 n의 배수 고르기/Programmers💻

GyuriKim·2023년 1월 4일

Programmers

목록 보기
1/10
post-thumbnail

n의 배수 고르기

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


제한사항

  • 1≤n≤10,000
  • 1≤numlist의 크기≤100
  • 1≤numlist의 원소≤100,000
nnumlistresult
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;
}                                     

풀고나서..

  • 코딩테스트가 익숙하지 않아서 그런지 푸는데 꽤 오래 애를 먹었다. n을 나누기 해서 0이 나오는 값을 answer에 push_back하였다. 머릿속에 있는 생각을 코드로 적어내는 것이 생각보다 어렵다.

C언어 풀이방법

  • 프로그래머스에 올라와있는 다른 사람들의 C언어 풀이방법이다. 나는 아직 malloc 개념을 잘 이해하지 못하여서, 아직 더 공부할 필요성을 절실히 느낀다.
#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;
}
profile
_〆(。。)

1개의 댓글

comment-user-thumbnail
2024년 10월 16일

열심히 하면 점점 더 좋아질거예요! o((>ω< ))o

답글 달기