반응형
링크
https://school.programmers.co.kr/learn/courses/30/lessons/12910
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명 및 제한 조건
나의 코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool Compare(int a,int b) {
return ( a < b );
}
vector<int> solution(vector<int> arr, int divisor){
vector<int> answer;
for(size_t i = 0; i < arr.size(); ++i){
if(arr[i] % divisor == 0) {
answer.push_back(arr[i]);
}
}
if(answer.size() == 0){
answer.push_back(-1);
} else {
sort(answer.begin(), answer.end(), Compare);
}
return answer;
}
채점 결과
다수 코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr, int divisor) {
vector<int> answer;
sort(arr.begin(), arr.end());
for (int i = 0; i < arr.size(); i++)
{
if (arr[i] % divisor == 0)
answer.push_back(arr[i]);
}
return answer.empty() ? vector<int>(1, -1) : answer;
}
반응형
'프로그래머스' 카테고리의 다른 글
[프로그래머스 C++] 행렬의 덧셈 (0) | 2022.10.09 |
---|---|
[프로그래머스 C++] 자릿수 더하기 (0) | 2022.10.07 |
[프로그래머스 C++] 문자열 다루기 기본 (0) | 2022.10.04 |
[프로그래머스 C++] x만큼 간격이 있는 n개의 숫자 (0) | 2022.10.03 |
[프로그래머스 C++] 가운데 글자 가져오기 (0) | 2022.10.01 |