-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_1431.java
More file actions
42 lines (39 loc) · 1.29 KB
/
boj_1431.java
File metadata and controls
42 lines (39 loc) · 1.29 KB
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
package sorting;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class boj_1431 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
List<String> list = new ArrayList<>();
for (int i = 0; i < N; i++) {
list.add(br.readLine());
}
list.sort((o1, o2) -> {
if (o1.length() != o2.length()) {
return o1.length() - o2.length();
} else {
int a = 0;
int b = 0;
for (int i = 0; i < o1.length(); i++) {
int num1 = o1.charAt(i) - '0';
int num2 = o2.charAt(i) - '0';
if (num1 > 0 && num1 < 10) {
a += num1;
}
if (num2 > 0 && num2 < 10) {
b += num2;
}
}
if (a == b) {
return o1.compareToIgnoreCase(o2);
}
return a - b;
}
});
list.forEach(System.out::println);
}
}