주어진 입력에서는 첫 번째 줄에는 수의 개수 N이 주어지고, 그 다음 줄부터 N개의 수가 주어집니다. 이 수를 오름차순으로 정렬하여 출력해야 합니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] numbers = new int[N];
// 수를 배열에 저장
for (int i = 0; i < N; i++) {
numbers[i] = Integer.parseInt(br.readLine());
}
// 배열을 오름차순으로 정렬
Arrays.sort(numbers);
// 정렬된 배열을 출력
StringBuilder sb = new StringBuilder();
for (int num : numbers) {
sb.append(num).append("\n");
}
System.out.print(sb);
}
}
이 코드는 주어진 N개의 수를 배열에 저장한 후, 배열을 오름차순으로 정렬하여 출력하는 기능을 수행합니다. 이를 통해 입력된 수를 정렬된 상태로 출력함으로써, 문제에서 요구하는 수를 오름차순으로 정렬하는 것을 해결합니다. 이 코드의 시간 복잡도는 입력된 수의 개수에 따라 O(NlogN)입니다. 따라서 대량의 입력에도 빠르게 동작할 수 있는 효율적인 알고리즘이라고 할 수 있습니다.
