2019-03-21 [JAVA]백준 10989번 - 수 정렬하기
22 Mar 2019 | java algorithm_study문제 출처
수 정렬하기 3 : https://www.acmicpc.net/problem/10989
문제 풀이 1
- Array 배열 라이브러이 사용 => 시간 초과
풀이 2
- 10000 크기의 배열 생성
- 입력값. = 배열 인덱스 => 배열 카운트 up
- 카운트된 배열 출력
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
int[] array = new int[10001];
for(int i=0; i<n; i++)
array[sc.nextInt()]++;
for(int i=1; i<=10000; i++){
for(int j=0; j<array[i]; j++)
System.out.println(i);
}
}
}
풀이 3
- BufferedReader , BufferedWriter 사용
전체 소스
import java.io.*;
import java.util.Scanner;
public class Baekjoon_10989 {
public static void main(String[] args) throws IOException {
Scanner sc =new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int[] array = new int[10001];
for(int i=0; i<n; i++)
array[Integer.parseInt(br.readLine())]++;
for(int i=1; i<=10000; i++){
for(int j=0; j<array[i]; j++)
bw.write(i+"\n");
}
br.close();
bw.close();
}
}
Comments