정렬 - 2108번
2108번: 통계학
첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 단, N은 홀수이다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.
www.acmicpc.net
문제점
1. -0으로 반올림되는 현상
-> 0.5를 더하거나, sum 자체를 double 자료형으로도 바꾸어보았지만 그냥 math.h를 이용해서 반올림하는게 제일 간단한 방법인 것 같아서 해당 방향으로 반올림에 대한 구현을 진행했다.
2. 최빈값을 카운팅하는 과정
-> 온전한 내 풀이로는 구현하지 못하고 다른 풀이를 참고했다. 추후 복습 필요
-> https://sedangdang.tistory.com/m/19
풀이
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable: 4996)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int compare(const void* a, const void* b)
{
if (*(int*)a > *(int*)b)
{
return 1;
}
else if (*(int*)a < *(int*)b)
{
return -1;
}
else
{
return 0;
}
}
int joongbok_check(int list[], int n)
{
int check[8001] = { 0 };
int i, idx, max = 0, cnt = 0;
for (i = 0; i < n; i++)
{
idx = list[i] + 4000;
check[idx] += 1;
if (check[idx] > max)
{
max = check[idx];
}
}
for (i = 0, idx = 0; i < 8001; i++)
{
if (check[i] == 0)
{
continue;
}
if (check[i] == max)
{
if (cnt == 0)
{
idx = i;
cnt += 1;
}
else if (cnt == 1)
{
idx = i;
break;
}
}
}
return idx - 4000;
}
int main(void)
{
int n;
double sum = 0;
scanf("%d", &n);
int* list;
list = (int*)calloc(n, sizeof(int));
for (int i = 0; i < n; i++)
{
scanf("%d", &list[i]);
sum += list[i];
}
qsort(list, n, sizeof(int), compare);
printf("%d\n", (int)round(sum / n));
printf("%d\n", list[n / 2]);
printf("%d\n", joongbok_check(list, n));
printf("%d", list[n - 1] - list[0]);
}
댓글