Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-2751번/Java] 수 정렬하기 2 본문

Algorithm

[백준-2751번/Java] 수 정렬하기 2

1984 2022. 11. 10. 22:22

* BufferedWriter 사용

import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int N = sc.nextInt();
		int[] numbers = new int[N];
		for (int i = 0; i < N; i++) {
			int num = sc.nextInt();
			numbers[i] = num;
		}

		Arrays.sort(numbers);
		for (int i = 0; i < N; i++) {
			bw.write(numbers[i] + "\n");
		}
		bw.flush();
		bw.close();
		sc.close();
	}

}

 

* System.out.println을 사용하면 시간초과

import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] numbers = new int[N];
		for (int i = 0; i < N; i++) {
			int num = sc.nextInt();
			numbers[i] = num;
		}

		Arrays.sort(numbers);
		for (int i = 0; i < N; i++) {
			System.out.println(numbers[i]);
		}

		sc.close();
	}

}
728x90
Comments