[Programmers] 가장 큰 수

Time Lapse :1hour 0min 0sec

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;
bool compare(int n1, int n2) {
string s1 = to_string(n1);
string s2 = to_string(n2);
s1 += s1; s1 += s1;
s2 += s2; s2 += s2;
int idx = 0;
while (idx != 4) {
if (s1[idx] > s2[idx])
return true;
else if (s1[idx] < s2[idx])
return false;
else
++idx;
}
return n1 <= n2 ? false : true;
}
string solution(vector<int> numbers) {
sort(numbers.begin(), numbers.end(), compare);
if (numbers[0] == 0)
return "0";
string answer = "";
for (int i = 0; i < numbers.size(); i++) {
answer += to_string(numbers[i]);
}
return answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/biggestNumber/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.