Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3ae9d9a

Browse files
issue sowon-dev#40 2108 통계학
1 parent 715eda9 commit 3ae9d9a

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

‎src/backjoon/_2108.java‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/2108
3+
// 통계학
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
8+
public class _2108 {
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
//sol memory 40392 runtime 264
13+
int N = Integer.parseInt(br.readLine());
14+
StringBuilder sb = new StringBuilder();
15+
// 입력값의 범위 : -4000 ~ 4000
16+
int[] arr = new int[8001];
17+
18+
int sum = 0;
19+
int max = Integer.MIN_VALUE;
20+
int min = Integer.MAX_VALUE;
21+
22+
// median 과 mode 는 -4000~4000 을 제외한 수로 초기화하면 된다.
23+
int median = 0;
24+
int mode = 0;
25+
26+
for(int i = 0; i < N; i++) {
27+
int value = Integer.parseInt(br.readLine());
28+
sum += value;
29+
arr[value + 4000]++;
30+
31+
if(max < value) {
32+
max = value;
33+
}
34+
if(min > value) {
35+
min = value;
36+
}
37+
}
38+
39+
int count = 0; // 중앙값 빈도 누적 수
40+
int mode_max = 0; // 최빈값의 최댓값
41+
42+
// 이전의 동일한 최빈값이 1번만 등장했을경우 true, 아닐경우 false
43+
boolean flag = false;
44+
45+
for(int i = min + 4000; i <= max + 4000; i++) {
46+
47+
if(arr[i] > 0) {
48+
49+
// <중앙값 찾기
50+
if(count < (N + 1) / 2) {
51+
count += arr[i]; // i값의 빈도수를 count 에 누적
52+
median = i - 4000;
53+
}
54+
55+
// 최빈값 찾기
56+
if(mode_max < arr[i]) {
57+
mode_max = arr[i];
58+
mode = i - 4000;
59+
flag = true; // 첫 등장이므로 true 로 변경
60+
}
61+
// 이전 최빈값 최댓값과 동일한 경우면서 한 번만 중복되는 경우
62+
else if(mode_max == arr[i] && flag == true) {
63+
mode = i - 4000;
64+
flag = false;
65+
}
66+
}
67+
}
68+
69+
System.out.println((int)Math.round((double)sum / N));
70+
System.out.println(median);
71+
System.out.println(mode);
72+
System.out.println(max - min);
73+
}
74+
}
75+
/*
76+
input
77+
5
78+
1
79+
3
80+
8
81+
-2
82+
2
83+
84+
output
85+
2
86+
2
87+
1
88+
10
89+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /