●문제

●입출력

사용 알고리즘 : introsort
풀이방식 vector<int> 를 to_string으로 문자열로 바꾼뒤 임시값
vector<string> temp임시값에 넣어준뒤
앞수 a 뒷수 b를 합쳤을때 더 큰수를 앞으로 "정렬"
풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(vector<int> numbers)
{
//비교 첫번째 numbers[i]의 값들중에 1의 자리수 값이 가장 큰 수
//비교 두번째 numbers[i]의 값들중에 10의 자리수 값이 더 큰 수
string answer = "";
vector<string> temp;
//number의 값을 string으로 변환해서 temp로 집어넣기
for (int i : numbers)
{
temp.push_back(to_string(i));
}
//a + b 와 b + a 인것중에 큰값을 도출
sort(temp.begin(), temp.end(), [](string& a, string& b)
{
//a + b 가 더 크면 a 먼저 정렬하라
return a + b > b + a;
});
if (temp[0] == "0")
{
return "0";
}
for (string j : temp)
{
answer += j;
}
return answer;
}
return a + b < b + a 인 경우 문자열 앞과 뒤 수를 합쳤을때 더 큰수를 앞으로 보내라 라는 의미
temp[0] == "0" 즉 모든수가 0 일경우를 계산해야하는거같은데 말장난인거같다..
레벨 : 2
점수 : 2
'C++ 프로그래머스 > Sort' 카테고리의 다른 글
| 프로그래머스(C++) - H-Index (1) | 2025.08.19 |
|---|---|
| 프로그래머스(C++) - k번째 수 (0) | 2025.08.18 |
| Algorithm - Sorting (0) | 2025.08.13 |
| 프로그래머스(C++) - 명예의 전당(1) (0) | 2025.07.27 |
