전공/Problem Solving

[알고리즘/문제풀이/BOJ 2108번] 통계학

caneo 2021. 6. 19. 14:23
728x90
반응형

계수 정렬에서 배열로 숫자의 빈도를 세는 방식을 응용해서 코드를 작성해보았습니다.

#include<stdio.h>
#include<math.h>

int N;
int sum;
int max = -5000, min = 5000;
int count[10000];
int temp;
int first_mode;
int second_mode=999999;
int mode;

//count sort
//ceil - 올림, floor - 내림
//반올림은 floor(x+0.5)
int main(void) {
	
	scanf("%d", &N);

	for (int i = 0; i < N; i++) {
		scanf("%d", &temp);
		sum += temp;
		if (temp > max) max = temp;
		if (temp < min) min = temp;

		temp = temp + 4002;
		count[temp]++;
	}

	printf("%d\n", (int)floor((double)sum / N + 0.5));

	temp = 0;

	for (int i = 0; i < 10000; i++) {
		temp = temp + count[i];
		if (temp >= (N / 2+1)) {
			printf("%d\n", i-4002);
			break;
		}
	}

	temp = 0;

	for (int i = 0; i < 10000; i++) {
		if (temp < count[i]) {
			temp = count[i];
			first_mode = i-4002;
			second_mode = 999999;
		}
		else if (temp == count[i] && second_mode == 999999) {
			second_mode = i-4002;
		}
	}

	if (second_mode == 999999) printf("%d\n", first_mode);
	else printf("%d\n", second_mode);

	printf("%d\n", max - min);
}

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

728x90
반응형