카운팅 정렬을 사용했다.
제공되는 숫자들의 절댓값이 4000을 넘지 않는다.
-4000 <= x <= 4000
idx는 0부터 시작하니까 4000 을 더해주면 0 ~ 8000이다.
그럼 배열의 length 는 8001.
산술평균은 입력받는 값들을 합해서 N으로 나눠주면 된다.
중앙값은 idx 0 부터 체크하면서 N / 2 번째의 숫자를 확인해주면 된다.
최빈값은 애매하게 2번째로 작은값을 출력을 하라고 한다.
처음에 최빈값이 몇개 나왔는지를 구하고, 2번째로 작은 값을 구해준다.
범위는 간단하게 제일 작은값을 min으로 넣어주고, 가장 큰 값을 max로 넣어서 max - min 해준다.
min과 max를 넣어주는 과정은 최빈값을 구하는 과정에서 list[i] > 0 이면 체크해서 넣어준다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <math.h> | |
/** | |
* https://www.acmicpc.net/problem/2108 | |
* BOJ 백준온라인져지 9426 중앙값 측정 풀이 | |
*/ | |
#define MAX_LENGTH 8001 | |
#define RoundOff(x, dig) (floor((x) * pow(10,dig) + 0.5) / pow(10,dig)) | |
int main(){ | |
int N,cnt,list[MAX_LENGTH] = {},idx,many[2]={},max=0,min=8000; | |
long long sum = 0; | |
scanf("%d", &N); | |
cnt = N; | |
while(cnt--){ | |
scanf("%d",&idx); | |
sum+=idx, list[idx+=4000]++; | |
} | |
idx = 0; | |
for(int i = 0; i<MAX_LENGTH; i++){ | |
if(many[0] < list[i]) many[0] = list[i], many[1] = i - 4000; | |
if(list[i]){ | |
max = (max < i) ? i : max; | |
min = (min > i) ? i : min; | |
} | |
} | |
cnt = 0; | |
for(int i = 0; i<MAX_LENGTH; i++){ | |
if(many[0] == list[i]){ | |
if(cnt){ | |
many[1] = i - 4000; | |
break; | |
} | |
cnt++; | |
} | |
} | |
for(int i = 0, length = N/2; i<=length; i++){ | |
while(list[idx]==0) idx++; | |
list[idx]--; | |
} | |
printf("%.lf\n",roundf(sum/(double)N)); | |
printf("%d\n",idx-4000); | |
printf("%d\n",many[1]); | |
printf("%d",max-min); | |
return 0; | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 1181 단어 정렬 풀이 (0) | 2017.11.23 |
---|---|
BOJ 백준온라인져지 1427 소트인사이드 풀이 (0) | 2017.11.22 |
BOJ 백준온라인져지 1786 찾기 풀이 (1) | 2017.11.19 |
BOJ 백준온라인져지 5525 IOIOI 풀이(KMP) (0) | 2017.11.16 |
BOJ 백준온라인져지 6064 카잉 달력 풀이 (0) | 2017.11.14 |