배열을 정렬하는 것은 쉽다. 수가 주어지면, 그 수의 각 자리수를 내림차순으로 정렬해보자.
첫째 줄에 정렬하려고 하는 수 N이 주어진다. N은 1,000,000,000보다 작거나 같은 자연수이다.
첫째 줄에 자리수를 내림차순으로 정렬한 수를 출력한다.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//입력 범위가 크기 때문에 String으로 입력 받음
String num = sc.next();
//String 각각의 숫자를 정수로 바꿔서 배열에 넣는다.
int list[] = new int[num.length()];
for(int i=0; i<num.length(); i++) {
list[i] = num.charAt(i)-'0';
}
//Selection Sort를 이용해 배열을 내림차순으로 정렬한다.
int most, temp;
for(int i=0; i<num.length()-1; i++) {
most = i;
for(int j=i; j<num.length(); j++)
if(list[j]>list[most]) most = j;
temp = list[i];
list[i] = list[most];
list[most] = temp;
}
//배열 출력하기
for(int i=0; i<num.length(); i++) {
System.out.print(list[i]);
}
}
}