[내 풀이]
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
long long solution(long long n) {
vector<int> vec;
string str = "";
while(n) // n이 0이면 멈춤
{
vec.push_back(n % 10);
n /= 10;
}
sort(vec.begin(), vec.end(), greater<>());
for(int c: vec)
{
str += to_string(c);
}
return stoll(str);
}
[다른 사람 풀이]
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
long long solution(long long n) {
long long answer = 0;
string str = to_string(n);
sort(str.begin(), str.end(), greater<char>());
answer = stoll(str);
return answer;
}
greater<>()은, sort 사용 시에 내림차순 할 때 compare을 도와주는 함수형 객제이다.
<>안에 형 지정을 해줘야 했지만 지금은 비워둬도 자동으로 채워진다.
#include <functional>
을 추가해줘야 한다.