-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ2108.java
More file actions
70 lines (62 loc) · 2.03 KB
/
BOJ2108.java
File metadata and controls
70 lines (62 loc) · 2.03 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
package main.week2.BOJ2108;
import java.util.*;
/**
* 통계학
* @author hazel
*/
public class BOJ2108 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int num = scanner.nextInt();
sum += num;
arr[i] = num;
}
//오름차순 정렬
Arrays.sort(arr);
//산술평균- 소수점 첫번째 자리 반올림
int avg = (int) Math.round((double) sum / arr.length);
System.out.println(avg);
//중앙값
System.out.println(arr[n / 2]);
//최빈값
System.out.println(mode(arr, n));
//범위
System.out.println(arr[n - 1] - arr[0]);
}
//최빈값
public static int mode(int[] arr, int n) {
Map<Integer, Integer> mp = new HashMap<>();
if (n == 1) { //값이 하나면 바로 리턴
return arr[0];
}
//map에 추가
for (int i = 0; i < n; i++) {
if (mp.containsKey(arr[i])) {
mp.put(arr[i], mp.get(arr[i]) + 1);
} else {
mp.put(arr[i], 1);
}
}
//System.out.println(mp.toString());
//value중 가장 큰 값
int maxValue = Collections.max(mp.values());
//System.out.println(maxValue);
ArrayList<Integer> arrayList = new ArrayList<>();
//value의 가장 많이 나온 값
for (Map.Entry<Integer, Integer> m : mp.entrySet()) {
if (m.getValue() == maxValue) { //같으면 모두 배열에 넣기
arrayList.add(m.getKey());
}
}
Collections.sort(arrayList);//오름차순 정렬
// 가장 많이 나온 값이 여러개일 때 두번째로 작은 값
if (arrayList.size() > 1)
return arrayList.get(1);
else // 가장 많이 나온 값이 하나일떄
return arrayList.get(0);
}
}