LANG/C++
[C++]프로그래머스 정수 내림차순으로 배치하기
혀니리리
2023. 5. 14. 20:10
728x90
내코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
long long solution(long long n) {
vector <int>a;
long long answer = 0;
while(n / 10 != 0)
{
a.push_back(n % 10);
n = n / 10;
}
a.push_back(n % 10);
sort(a.begin(), a.end());
for (int i = a.size() - 1; i >= 0; i--)
{
answer += a.at(i);
if (i == 0)
break;
answer *= 10;
}
return answer;
}
똑똑이코드
#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;
}
wow!
숫자를 string으로 변환-> string을 char 형태로 오름차순으로 정렬 -> string을 다시 longlong으로 stoll 함수 통해 변환
728x90