C++:: 프로그래머스 < 나누어 떨어지는 숫자 배열 >

jahlee·2023년 8월 14일
0

프로그래머스_Lv.1

목록 보기
73/75
post-thumbnail

제목 그대로 주어진 숫자 배열에서 주어진 divisor로 나누어지면 정답배열에 넣고 오름차순으로 정렬해서 리턴해 주면 되는 문제이다.

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> solution(vector<int> arr, int divisor) {
    vector<int> answer;
    for (auto target : arr) {
        if (target % divisor == 0) answer.push_back(target);
    }
    if (answer.empty()) return {-1};
    sort(answer.begin(), answer.end());
    return answer;
}

0개의 댓글