단계별로 풀어보기 > 심화 2 > 통계학
https://www.acmicpc.net/problem/2108
N개의 수가 주어질 때, 다음의 통계값을 구하여라
산술평균 : N개의 수들의 합을 N으로 나눈 값
중앙값 : N개의 수들을 증가하는 순서로 나열했을 경우 그 중앙에 위치하는 값
최빈값 : N개의 수들 중 가장 많이 나타나는 값
범위 : N개의 수들 중 최댓값과 최솟값의 차이

N개의 수를 입력 받아서 배열로 만들어, 오름차순으로 정렬한다.
산술평균 -> 주어진 수를 다 더한 뒤, 더한 값과 N개로 나눈 값을 소수 첫번째 자리에서 반올림하여 return 한다.
중앙값 -> 정렬한 배열 중앙에 있는 값을 return 한다.
최빈값 -> 수들의 빈도를 기록하기 위해 HashMap을 생성하고, 빈도를 기록한다.
기록한 빈도 중 가장 빈도가 높은 수와 빈도를 저장한다.
해당 빈도와 동일한 빈도를 가진 수들을 모두 list에 저장하고, 그 중 2번째로 작은 수를 출력한다. (단, hasmap은 순서 보장이 안되므로 정렬해야한다.)
범위 -> 정렬된 배열의 첫번째 값과 마지막 값의 차이를 return 한다.
import java.io.*;
import java.util.*;
import static java.lang.Math.round;
public class 통계학 {
public static int arithmetic_mean(int[] arr){
double result = 0;
for (int i : arr) {
result += i;
}
return (int) round(result /arr.length);
}
public static int median(int[] arr){
int result = arr[arr.length/2];
return result;
}
public static int mode(int[] arr){
HashMap<Integer,Integer> hm = new HashMap<>();
List<Integer> list = new ArrayList<>();
for (int i : arr) {
hm.put(i,hm.getOrDefault(i,0)+1);
}
int max = Integer.MIN_VALUE;
int maxKey = 0;
for (int i : hm.keySet()) {
if(max < hm.get(i)){
max = hm.get(i);
maxKey = i;
}
}
for (int i : hm.keySet()) {
if(hm.get(i) == max){
list.add(i);
}
}
if(list.size() == 1){
return list.get(0);
} else{
Collections.sort(list);
return list.get(1);
}
}
public static int range(int[] arr){
return arr[arr.length -1] - arr[0];
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
for(int i =0; i<N; i++){
arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
int arithmetic_mean = arithmetic_mean(arr);
int median = median(arr);
int mode = mode(arr);
int range = range(arr);
StringBuilder sb = new StringBuilder();
sb.append(arithmetic_mean).append("\n").append(median).append("\n").append(mode).append("\n").append(range);
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
}
Review
import java.io.*;
import java.util.*;
import static java.lang.Math.round;
public class 통계학_review {
public static int arithmetic_mean(int[] arr){
// 산술평균 : N개의 수들의 합을 N으로 나눈 값(소수점 이하 첫째 자리에서 반올림)
double result = 0;
for (int i : arr) {
result +=i;
}
return (int) round(result/arr.length);
}
public static int median(int[] arr){
// 중앙값 : N개의 수들을 증가하는 순서로 나열했을 경우 그 중앙에 위치하는 값
return arr[arr.length/2];
}
public static int mode(int[] arr){
// 최빈값 : N개의 수들 중 가장 많이 나타나는 값
HashMap<Integer,Integer> hm = new HashMap<>();
List<Integer> list = new ArrayList<>();
for (int i : arr) {
hm.put(i,hm.getOrDefault(i,0)+1);
}
int maxValue = Integer.MIN_VALUE;
int maxKey;
for (Integer i : hm.keySet()) {
if(hm.get(i) > maxValue){
maxKey = i;
maxValue = hm.get(i);
}
}
for (Integer i : hm.keySet()) {
if(hm.get(i) == maxValue){
list.add(i);
}
}
if(list.size() == 1){
return list.get(0);
} else {
Collections.sort(list);
return list.get(1);
}
}
public static int range(int[] arr){
// 범위 : N개의 수들 중 최댓값과 최솟값의 차이
return arr[arr.length-1] - arr[0];
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int arr[] = new int[N];
for(int i = 0; i<N; i++){
arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
sb.append(arithmetic_mean(arr)).append("\n")
.append(median(arr)).append("\n")
.append(mode(arr)).append("\n")
.append(range(arr)).append("\n");
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
}
산술평균을 계산할 때, N개의 수를 double 변수에 넣지 않고, int 변수에 넣었더니 틀렸다.
int 변수에 넣으면 소수자리는 다 날아가기 때문에 주의해야한다.
Review
최빈값에서 빈도수 제일 높은 것이 1개 일 경우를 주의해야한다.
