https://programmers.co.kr/learn/courses/30/lessons/42746

 

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

0 또는 양의 정수가 주어졌을 때, 정수를 이어 붙여 만들 수 있는 가장 큰 수를 알아내 주세요. 예를 들어, 주어진 정수가 [6, 10, 2]라면 [6102, 6210, 1062, 1026, 2610, 2106]를 만들 수 있고, 이중 가장 큰 ��

programmers.co.kr

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
 
bool cmp(const string& a, const string& b) {
    return a + b > b + a;
}
 
string solution(vector<int> numbers) {
    string answer = "";
 
    vector<string> ns;
    for (int n : numbers) ns.push_back(to_string(n));
    sort(ns.begin(), ns.end(), cmp);
    for (auto s : ns) answer += s;
    if (answer[0== '0') answer = "0";
    return answer;
}
cs

 

 

 

 

 

https://github.com/has2/Algorithm/blob/master/programmers/level2/%ED%81%B0%20%EC%88%98%20%EB%A7%8C%EB%93%A4%EA%B8%B0.cpp

+ Recent posts