문제 & 예시
소스코드
import java.util.Arrays;
// 정수 내림차순으로 배치하기
public class test12 {
public static void main(String[] args) {
Solution12 sol = new Solution12();
long n = 118372;
System.out.println(sol.solution(n));
}
}
class Solution12 {
public long solution(long n) {
long answer = 0;
long temp = 0; // 자리수
int cnt = 0; // 증가 변수
String answer_temp = ""; // 문자열로 붙이기위한 변수
// int의 길이를 Math.log10(n) + 1을 통해서 얻음
long[] arr = new long[(int)Math.log10(n)+1];
// n값을 쪼개기 위한 반복문
while(n>0) {
temp = n % 10;
n = n / 10;
arr[cnt++] = temp;
}
// 오름차순으로 정렬
Arrays.sort(arr);
// 내림차순으로 다시 넣어주는 역할
for(int i=arr.length - 1;i>=0;i--) {
answer_temp += arr[i];
}
// String값을 long으로 변환
answer = Long.parseLong(answer_temp);
return answer;
}
}