프로그래머스 - level_2 - 가장 큰 수

ggh·2022년 5월 11일
0

프로그래머스  

목록 보기
2/3

프로그래머스 - level_2 - 가장 큰 수

문제


코딩테스트 연습 - 가장 큰 수

SOL


  • to_string으로 변환한 두 정수를 합한 값이 큰 값을 기준으로 정렬하기
    • ex) sum (”3”, “11”) ~> 311
    • sum (”11", “3”) ~> 113

구현


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
// 앞자리 기준으로 
// 내림차순 정렬 
// 문자열에 저장 

bool compare(int a, int b)
{ 
    string tmp_1 = to_string(a) + to_string(b);
    string tmp_2 = to_string(b) + to_string(a);
    return  tmp_1 > tmp_2;
}
string solution(vector<int> numbers) {
    string answer = "";
    sort(numbers.begin(), numbers.end(), compare);
    for(auto el : numbers)
        answer += to_string(el);
    if(answer[0] == '0')
        answer = "0";
    return answer;
}

Reference


C++ STL sort ( )

profile
See you in a minute. : )

0개의 댓글