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 |
'Problem-solving > 프로그래머스' 카테고리의 다른 글
프로그래머스 - 문자열 내 마음대로 정렬하기 (C++) (0) | 2020.03.05 |
---|---|
프로그래머스 - 두 정수 사이의 합 (C++) (0) | 2020.03.04 |
프로그래머스 - 같은 숫자는 싫어 (C++) (0) | 2020.03.04 |
프로그래머스 - 체육복 (C++) (0) | 2020.02.29 |
프로그래머스 - 모의고사 (C++) (0) | 2020.02.28 |
댓글