백준 2750번 www.acmicpc.net/problem/2750

 

 

풀이

 

Collection 중 Arraylist.sort() 메서드를 이용해서 풀이했다. 메서드를 통해 오름차순 정렬이 가능하다. 값은 Integer 타입으로 받아와야 음수 값이 들어와도 바르게 정렬된다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
 
public class sort_2750 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        sc.nextLine();
        List<Integer> intList = new ArrayList<>();
        
        for (int i=0; i<a; i++) {
            intList.add(Integer.parseInt(sc.nextLine()));
        }
        
        Collections.sort(intList);
        
        
        for (int i=0; i<a; i++) {
            System.out.println(intList.get(i));
        }
 
    }
 
}
cs
댓글