Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 알고리즘
- 수학
- 안드로이드
- 다이나믹프로그래밍
- 프로그래밍
- 선택알고리즘
- 선택정렬
- 동적프로그래밍
- 퀵정렬
- java
- 자바
- 백트래킹
- 백준
- 힙정렬
- 자료구조
- DP
- 병합정렬
- 프로그래밍언어
- 계수정렬
- 삽입정렬
- 정수론
- 코딩테스트
- 동적계획법
- C++
- 기수정렬
- 정렬
- 버블정렬
- SNS
- Median of Medians
- 재귀
Archives
- Today
- Total
MODE::CREATIVE
[백준][c++] 2108번: 통계학 본문
https://www.acmicpc.net/problem/2108
문제 해석
- 산술평균, 중앙값, 최빈값, 범위 구하기
알고리즘 분류
- 수학
- 정렬
풀이
- 배열의 값을 모두 더한후 나눠 산술평균을 구한다
- 배열을 정렬해 중앙값, 범위를 구한다
- 정수의 절대값이 4000까지인 점을 참고하여 최빈값을 구한다
코드
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <unordered_map>
using namespace std;
int n;
double sum = 0;
int arr[800001] = {0, };
void qSort(int A[], int start, int end) {
if (start >= end) return;
int pivot = start;
int i = start + 1;
int j = end;
while (i <= j) {
while (i <= end && A[i] <= A[pivot]) i++;
while (j > start && A[j] >= A[pivot]) j--;
if (i > j) {
swap(A[j], A[pivot]);
} else {
swap(A[i], A[j]);
}
}
qSort(A, start, j - 1);
qSort(A, j + 1, end);
}
int mode(){
int result;
int count = 0;
int cnt2[8001] = {0};
for (int i = 0; i < n;i++){
cnt2[arr[i] + 4000]++;
}
int max_mode = *max_element(cnt2, cnt2 + 8001);
for (int i = 0; i < 8001;i++){
if(cnt2[i] == max_mode){
count++;
result = i - 4000;
}
if(count == 2){
break;
}
}
return result;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
sum += arr[i];
}
qSort(arr, 1, n);
double rounded_avg = round(sum / n);
if (rounded_avg == -0.0) {
rounded_avg = 0.0;
}
cout << rounded_avg << endl; // 평균
cout << arr[(n + 1) / 2] << endl; // 중앙값
cout << mode() << endl; //최빈값
int range = arr[n] - arr[1];
cout << range << endl; // 범위
return 0;
}
'BOJ' 카테고리의 다른 글
[백준][c++] 1929번: 소수 구하기 (1) | 2024.10.02 |
---|---|
[백준][c++] 1966번: 프린터 큐 (0) | 2024.10.02 |
[백준][c++] 2193번: 이친수 (0) | 2024.09.22 |
[백준][c++] 2579번: 계단 오르기 (0) | 2024.09.22 |
[백준][c++] 2606번: 바이러스 (2) | 2024.09.22 |