문제 정보
문제
함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다.
제한 조건
n은 1이상 8000000000 이하인 자연수입니다.
입출력 예
입출력 예
n | return
118372 | 873211
풀이 코드
정수 내림차순으로 배치하기.cpp
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
long long solution(long long n) {
long long answer = 0;
string fi_word;
string word = to_string(n);
vector<int> num_list;
for (int i = 0; i < word.size(); i++)
{
//아스키를 활용하여 '-0'을 해줘야 알맞은 int값이 넘어감.
int num = word[i] - '0';
num_list.push_back(num);
}
sort(num_list.begin(), num_list.end());
for (int i = num_list.size() - 1; i >= 0; i--) {
string cur_word = to_string(num_list[i]);
fi_word += cur_word;
}
answer = stoll(fi_word);
return answer;
}
댓글
GitHub 계정으로 의견을 남길 수 있습니다.
GitHub에서 댓글 삭제