arr를 하나씩 검사하면서 나누어떨어지는지 확인한 후, answer가 비어있다면 -1를 넣어주어
sort로 정렬한 후 리턴해주면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#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);
}
sort(answer.begin(),answer.end());
return answer;
}
|
cs |