소트인사이드

개굴이·2023년 9월 26일
0

코딩테스트

목록 보기
33/58
post-thumbnail

백준 1427번 소트인사이드

배열을 정렬하는 것은 쉽다. 수가 주어지면, 그 수의 각 자리수를 내림차순으로 정렬해보자.

입력

첫째 줄에 정렬하려고 하는 수 N이 주어진다. N은 1,000,000,000보다 작거나 같은 자연수이다.

출력

첫째 줄에 자리수를 내림차순으로 정렬한 수를 출력한다.

소스

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		String input = br.readLine(); //정렬할 수
		int[] words = new int[input.length()];
		for(int i = 0; i < input.length(); i++)
			words[i] = Integer.parseInt(input.substring(i, i + 1));
		for(int i = 0; i < input.length(); i++) {
			int max = -1;
			int index = -1;
			for(int j = i + 1; j < input.length(); j++)
				if(words[j] > max) {
					max = words[j];
					index = j;
				}
			if(words[i] < max) {
				words[index] = words[i];
				words[i] = max;
			}
		}
		for(int i : words)
			bw.write(i + "");
		bw.flush();
		bw.close();
	}

	
}

0개의 댓글