웅재의 코딩세상
[프로그래머스] 나누어 떨어지는 숫자 배열 본문
- 풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr, int divisor) {
vector<int> answer;
int v=0;
for(int i=0; i<arr.size();i++){
if(arr[i] % divisor == 0) {
answer.push_back(arr[i]);
v++;
}
}
if(v==0) answer.push_back(-1);
sort(answer.begin(),answer.end());
return answer;
}
// return answer.empty() ? vector<int>(1, -1) : answer; empty함수 활용 가능
반응형
'코딩테스트 > 프로그래머스 - LV 1' 카테고리의 다른 글
[프로그래머스] 가운데 글자 가져오기 (0) | 2024.02.09 |
---|---|
[프로그래머스] 같은 숫자는 싫어 (0) | 2024.02.09 |
[프로그래머스] 두 정수 사이의 합 (0) | 2024.02.09 |
[프로그래머스] 문자열 내 p와 y의 개수 (0) | 2024.02.09 |
[프로그래머스] 문자열 내림차순으로 배치하기 (0) | 2024.02.09 |