-
나누어 떨어지는 숫자 배열 [C++]2018~2019/Level 1 2018. 10. 1. 22:24
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
#include <string> #include <vector> #include <algorithm> using namespace std; vector<int> solution(vector<int> arr, int divisor) { vector<int> answer; bool check = false; for(int i=0; i<arr.size(); i++) { if(arr[i] % divisor == 0) { answer.push_back(arr[i]); check = true; } } if(!check) answer.push_back(-1); sort(answer.begin(), answer.end()); return answer; }
- for문을 돌면서 배열의 요소가 divisor로 나눠지면 answer에 push back한다.
- 나누어 떨어지는 요소가 하나도 없으면 배열에 -1을 담아야 하므로 check 변수를 통해 확인한다.
- 오름차순으로 정렬하여 반환한다.
'2018~2019 > Level 1' 카테고리의 다른 글
문자열 내 마음대로 정렬하기 [JavaScript] (0) 2018.10.01 두 정수 사이의 합 [JavaScript] (0) 2018.10.01 같은 숫자는 싫어 [C++] (0) 2018.10.01 가운데 글자 가져오기 [Python3] (0) 2018.10.01 2016년 [JavaScript] (0) 2018.10.01