[Java | 백준 2751번] 수 정렬하기 - 2
· 테크· Java
Java
백준 2751번 www.acmicpc.net/problem/2751
풀이
처음부터 Collection.sort() 메서드와 BufferedReader 를 사용했지만 시간초과 가 나왔었다.
이 문제의 주의점은 입출력, 오름차순 정렬 모두 시간 소비가 되지 않도록 조절할 수 있어야 한다는 점이다.
따라서 출력시에는 BufferedWriter 를 사용했더니 정답이 되었다. 코드 실행에 따른 시간을 줄이기 위한 방법이 무엇인지 생각해볼 수 있는 문제이다.
|
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
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int a = Integer.parseInt(br.readLine());
List<Integer> intList = new ArrayList<>();
for (int i=0; i<a; i++) {
intList.add(Integer.parseInt(br.readLine()));
}
Collections.sort(intList);
for(int i=0;i<a;i++) {
bw.write(intList.get(i)+"\n");
}
bw.flush();
bw.close();
br.close();
}
}
|
cs |
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?
1–5 / 319