[프로그래머스] 특이한 정렬 - c++

삼식이·2025년 5월 10일
0

알고리즘

목록 보기
48/81

특이한 정렬

이 문제는 커스텀 함수를 써서 풀었다.
그동안 제대로 써본적이 없었는데 스스로 생각해내서 활용해서 좋았다!!
🙂🙂🙂

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> numlist, int n) {
    vector<int> answer;
    vector<pair<int, int>> tmp;
    for(int i=0; i<numlist.size(); i++) {
        tmp.push_back({abs(n-numlist[i]), numlist[i]});
    }
    
    sort(tmp.begin(), tmp.end(), [](
        pair<int, int> a, pair<int, int> b){
        if (a.first == b.first) {
            return a.second > b.second;
        }
        return a.first < b.first;
    });
        
    for(int i=0; i<tmp.size(); i++) {
        answer.push_back(tmp[i].second);
    }
    
    return answer;
}

0개의 댓글