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);
}
728x90
반응형
'전공 > Problem Solving' 카테고리의 다른 글
[알고리즘/문제풀이/BOJ 10814번] 나이 순 정렬 (0) | 2021.06.20 |
---|---|
[알고리즘/문제풀이/BOJ 1181번] 단어 정렬 (0) | 2021.06.20 |
[알고리즘/문제풀이/BOJ 11651번] 좌표 정렬하기 2 (0) | 2021.06.19 |
[알고리즘/문제풀이/BOJ 11650번] 좌표 정렬하기 (0) | 2021.06.19 |
[알고리즘/문제풀이/BOJ 1427번] 소트인사이드 (0) | 2021.06.19 |