Coding Test - cpp/Sort
[백준 2108: 통계학] - C++
에드윈H
2021. 3. 15. 14:37
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
int main() {
vector<int> arr;
vector<int> arr2(8001, 0);
int n;
double totalValue=0;
int maxValue =0;
int temp = 0;
int mode = 0;
bool isSecond = false;
cin >> n;
int input;
for (int i = 0; i < n; i++)
{
cin >> input;
arr.push_back(input);
totalValue += input;
temp = (arr[i] <= 0) ? abs(arr[i]) : arr[i] + 4000;
arr2[temp]++;
if (arr2[temp] > maxValue)
maxValue = arr2[temp];
}
sort(arr.begin(), arr.end());
double sansool = round(static_cast<double>(totalValue) /static_cast<double>(n));
int bumwee = arr.back() - arr.front();
int center = arr[n / 2];
for (int i = -4000; i < 4001; i++)
{
temp = i <= 0 ? abs(i) : i + 4000;
if (arr2[temp] == maxValue)
{
mode = i;
if (isSecond)
break;
isSecond = true;
}
}
cout << sansool << endl;
cout << center << endl;
cout << mode << endl;
cout << bumwee << endl;
return 0;
}