본문 바로가기
IT/알고리즘

백준 통계학문제 2108 자바

by 어센트 2020. 2. 15.

 

배운것

    1. 소수첫째자리 반올림  Math.round()
    2. 절대값 Math.abs();
    3. 음수범위를 포함하여 인덱스에 넣을때는 공통된 어떤 수를 더하여 가장 작은 수가 0이 되도록 만들어 주는 것이 편하다
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
package 정렬;
 
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
 
public class 통계학 {
    static int[] nums ;
    static int[] chk = new int[8001];
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        nums = new int[n];
        double avg = 0;
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(br.readLine());
            chk[nums[i]+4000]++;
            avg += nums[i];
        }
        int maxIndex = 0;
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i <8001; i++) {
            if(chk[maxIndex]<chk[i]){
                maxIndex = i;
                list.clear();
            }
            else if(chk[i]!= 0 &&chk[i] == chk[maxIndex])
            {
                list.add(i-4000); //두번째 최빈값부터 계속 리스트에 추가
            }
        }
        Arrays.sort(nums);
        System.out.println((int)Math.round((double)(avg/n))) ;
        System.out.println(nums[n/2]);
        if(list.size()!= 0)
            System.out.println(list.get(0));
        else
            System.out.println(maxIndex-4000);
        System.out.println(nums[n-1]-nums[0]);
    }
}
 
cs
  1.