티스토리 뷰

PS/BOJ C++

2108번 - 통계학

zpqmdh 2021. 10. 10. 04:17

https://www.acmicpc.net/problem/2108

 

2108번: 통계학

첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 단, N은 홀수이다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.

www.acmicpc.net

#include <iostream>

#include <algorithm>

#include <cmath>

using namespace std;

int main()

{

  int N;

  cin >> N;

  int sum = 0, max, cnt=0;;

  int arr[N]; //입력받는 수

  int bindo[8001] = {0, }; //최빈값을 구하기 위한 배열

  bool not_first = false; //같은 값이 나오는 지 확인하기 위한 bool형 변수

  for(int i=0; i<N; i++)

  { 

    cin >> arr[i];

    sum += arr[i];

    bindo[arr[i]+4000]++;

  }

  sort(arr, arr+N); //오름차순 정렬

  for(int i=0; i<8001; i++)

  {

    if(bindo[i] == cnt)

    {

      if(not_first)

      {

        max = i-4000;

        not_first = false;

      }

    }

    if(bindo[i] > cnt)

    {

      max = i-4000;

      cnt = bindo[i];

      not_first = true; //나온 횟수

    }

   

  }

  cout << round((double)sum / N) << '\n'; //산술평균

  cout << arr[N/2] << '\n'; //중앙값

  cout << max << '\n'; //최빈값

  cout << arr[N-1]-arr[0] << '\n'; //범위

  return 0;

}

 

'PS > BOJ C++' 카테고리의 다른 글

18870번 - 좌표 압축  (0) 2021.10.16
9507번 - Generations of Tribbles  (0) 2021.10.13
11653번 - 소인수분해  (0) 2021.10.06
1059번 - 좋은 구간  (0) 2021.10.03
3036번 - 링  (0) 2021.09.26
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
글 보관함