본문 바로가기
Problem-solving/프로그래머스

프로그래머스 - 나누어 떨어지는 숫자 배열 (C++)

by taehee.kim.dev 2020. 3. 4.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
 
 
 
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> solution(vector<int> arr, int divisor) {
    vector<int> answer;
 
    for(int i = 0; i < arr.size(); i++){
        if(arr[i] % divisor == 0){
            answer.push_back(arr[i]);
        }
    }
 
    if(answer.empty()){
        answer.push_back(-1);
    }else{
         sort(answer.begin(), answer.end());
    }
 
    return answer;
}
 
 
 
int main(void){
    ios_base :: sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);
 
    vector<int> arr{3,2,6};
    int divisor = 10;
    
    vector<int> answer = solution(arr, divisor);
 
    for(int i = 0; i < answer.size(); i++){
        cout<<answer[i]<<" ";
    }
 
    return 0;
}
cs

댓글