프로그래머스(Programmers)

Level 1 > 나누어 떨어지는 숫자 배열

cepiloth 2018. 8. 8. 15:05
반응형

https://programmers.co.kr/learn/courses/30/lessons/12910


1. 문제


2. 알고리즘

키워드 - 구현, 배열


3. 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> solution(vector<int> arr, int divisor) {
    vector<int> answer;
    
    sort(arr.begin(), arr.end());
    
    const int size = arr.size();
    for(int i=0; i<size; i ++) {
        if(arr[i] % divisor == 0)
            answer.push_back(arr[i]);
    }
    
    if(answer.size() == 0) {
        answer.push_back(-1);
    }
    return answer;
}
cs

반응형