알고리즘

C++ 알고리즘 - 백준 2108 통계학

마루설아 2025. 1. 4. 15:10

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

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

int num[8002];

int main(void) {
	// C++ Init
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int input1, input2, sum = 0, max;
	int answer2, answer3, answer4;
	double answer1;
	vector<int> v, v2;
	cin >> input1;

	for (int i = 0; i < input1; i++) {
		cin >> input2;
		v.push_back(input2);
	}

	sort(v.begin(), v.end());

	for (int i = 0; i < v.size(); i++) {
		sum += v[i];
		num[v[i] + 4000]++;
	}

	for (int i = 0; i < 8002; i++) {
		num[i]--;
	}

	max = num[0];

	for (int i = 1; i < 8002; i++) {
		if (max < num[i]) max = num[i];
	}

	for (int i = 0; i < 8002; i++) {
		if (num[i] == max) v2.push_back(i - 4000);
	}

	answer1 = round(sum / (double)v.size());
	answer2 = v[v.size() / 2];
	answer3 = v2[0];
	answer4 = abs(v[v.size() - 1] - v[0]);

	if (answer1 == -0) answer1 = 0;
	if (v2.size() > 1) answer3 = v2[1];

	cout << answer1 << endl;
	cout << answer2 << endl;
	cout << answer3 << endl;
	cout << answer4 << endl;
}