Skip to content

Commit 399de82

Browse files
committed
Add: algorithm of baekjoon
1 parent 468752d commit 399de82

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.imsejin.study.baekjoon.silver;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.Arrays;
6+
7+
public class P18110 {
8+
9+
public static void main(String[] args) throws Exception {
10+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
11+
int[] difficulties = new int[Integer.parseInt(reader.readLine())];
12+
13+
for (int i = 0; i < difficulties.length; i++) {
14+
int difficulty = Integer.parseInt(reader.readLine());
15+
difficulties[i] = difficulty;
16+
}
17+
18+
int answer = solve(difficulties);
19+
System.out.println(answer);
20+
}
21+
}
22+
23+
static int solve(int[] difficulties) {
24+
if (difficulties.length == 0) return 0;
25+
26+
final int excluded = -1;
27+
Arrays.sort(difficulties);
28+
int excludedCount = (int) Math.round(difficulties.length * 0.15);
29+
30+
for (int i = 0; i < excludedCount; i++) {
31+
difficulties[i] = excluded;
32+
difficulties[difficulties.length - 1 - i] = excluded;
33+
}
34+
35+
int sum = 0;
36+
int count = 0;
37+
for (int difficulty : difficulties) {
38+
if (difficulty == excluded) {
39+
continue;
40+
}
41+
42+
sum += difficulty;
43+
count++;
44+
}
45+
46+
return (int) Math.round((double) sum / count);
47+
}
48+
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.imsejin.study.baekjoon.silver
2+
3+
import spock.lang.Specification
4+
5+
class P18110Spec extends Specification {
6+
7+
def "test"() {
8+
when:
9+
def actual = P18110.solve(difficulties as int[])
10+
11+
then:
12+
actual == expected
13+
14+
where:
15+
difficulties | expected
16+
[1, 5, 5, 7, 8] | 6
17+
[1, 13, 12, 15, 3, 16, 13, 12, 14, 15] | 13
18+
}
19+
20+
}

0 commit comments

Comments
 (0)