BOJ 1755. 숫자놀이
sort
- 숫자의 영단어를 String 배열에 저장
- 문자열을 사전순으로 정렬하는 compareTo() override
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
import java.io.*;
public class Main {
static int M, N;
static class Node implements Comparable<Node> {
int num;
String str;
Node(int num, String str) {
this.num = num;
this.str = str;
}
@Override
public int compareTo(Node o) {
return this.str.compareTo(o.str);
}
}
static void solution() {
List<Node> list = new ArrayList<>();
for (int i = M; i <= N; i++) {
StringBuilder sb = new StringBuilder();
char[] numStr = Integer.toString(i).toCharArray();
for (int j = 0; j < numStr.length; j++)
sb.append(map[numStr[j] - '0']).append(" ");
sb.setLength(sb.length()-1);
list.add(new Node(i, sb.toString()));
}
Collections.sort(list);
StringBuilder out = new StringBuilder();
for (int i = 0, cnt = 0; i < list.size(); i++, cnt++) {
String last = " ";
if (cnt == 9) {
last = "\n";
cnt = -1;
}
out.append(list.get(i).num).append(last);
}
System.out.print(out);
}
static String[] map = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine(), " ");
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
solution();
}
}