[백준] 2750번 - 수 정렬하기 [Java][C++]
[백준] 2750번 - 수 정렬하기 [Java][C++]
1. 문제 풀이
주어진 $N$ 개의 수를 정렬하는 문제로 수의 개수도, 수의 범위도 작아서 어떤 정렬을 활용해도 된다.
2. 코드
1. 정렬 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
for (int n : arr) {
sb.append(n).append("\n");
}
System.out.println(sb);
}
}
2. 정렬 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> v(n);
for (int& x : v) cin >> x;
sort(v.begin(), v.end());
for (int x : v) {
cout << x << '\n';
}
}
3. 풀이 정보
1. 정렬 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 116 ms | 14480 KB | 583 B |
2. 정렬 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2020 KB | 283 B |
This post is licensed under CC BY 4.0 by the author.