Repository navigation
Expand file tree
/
Copy pathBOJ_2108.java
More file actions
76 lines (60 loc) · 1.95 KB
/
Copy pathBOJ_2108.java
File metadata and controls
76 lines (60 loc) · 1.95 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package baekjoon.silver;
import java.io.*;
public class BOJ_2108 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
// 입력
// 범위 : -4,000 ~ 4,000
// 개수 : 1 ~ 500,000
int N = Integer.parseInt(br.readLine());
int[] array = new int[8001];
// 산술평균 : sum / length
// 중앙값 : medium
// 최빈값 : mode
// 범위 : max - min
int sum = 0;
int medium = 8002;
int mode = 8002;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
// 배열 초기화
for (int i = 0; i < N; i++) {
int value = Integer.parseInt(br.readLine());
sum += value;
array[value+4000]++;
if(max < value)
max = value;
if(value < min)
min = value;
}
int count = 0; // 중앙값 빈도 누적 수
int modeMax = 0; // 최빈값의 최댓값
boolean flag = false;
for (int i = min + 4000; i <= max + 4000; i++) {
if(array[i] == 0)
continue;
// 중앙값 찾기
if (count < (N + 1) / 2) {
count += array[i];
medium = i - 4000;
}
// 최빈값 찾기
if(modeMax < array[i]) {
modeMax = array[i];
mode = i - 4000;
flag = true;
}
else if (modeMax == array[i] && flag) {
mode = i - 4000;
flag = false;
}
}
System.out.println(Math.round((double)sum / N));
System.out.println(medium);
System.out.println(mode);
System.out.println(max - min);
bw.flush();
bw.close();
}
}