알고리즘/알고리즘 문제풀이

프로그래머스: 가장 큰 수

mint* 2023. 3. 15. 22:50
728x90
#include <bits/stdc++.h>

using namespace std;

bool cmp(const string a, const string b){
    if(b+a<a+b) return true;
    else return false;
}

string solution(vector<int> numbers) {
    string answer = "";
    vector<string> s_vec;
    for(int n:numbers) s_vec.push_back(to_string(n));
    sort(s_vec.begin(), s_vec.end(), cmp);
    for(string s:s_vec) answer += s;
    //00000일경우
    if(answer[0]=='0') return "0";
    return answer;
}

 

string 을 사용하여 비교하면 아스키 값 순서대로 비교된다.

 

728x90