-
Notifications
You must be signed in to change notification settings - Fork 166
/
basic-statistics-warmup-2.cpp
53 lines (51 loc) · 1.14 KB
/
basic-statistics-warmup-2.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <iomanip>
#include <vector>
#include <numeric>
#include <algorithm>
#include <map>
using namespace std;
vector<int> v;
map<int, int> m;
int main() {
int n;
cin >> n;
cout.setf(ios::fixed);
cout.precision(1);
for (int i = 0; i < n; i++) {
int num;
cin >> num;
v.push_back(num);
}
double mean = accumulate(v.begin(), v.end(), 0) / (double)n;
cout << mean << endl;
sort(v.begin(), v.end());
double median;
if (n % 2 == 1)
median = v[n / 2];
else
median = (v[n / 2 - 1] + v[n / 2]) / 2.0;
cout << median << endl;
for (int i = 0; i < n; i++) {
auto it = m.find(v[i]);
if (it != m.end())
(it->second)++;
else
m.insert(make_pair(v[i], 1));
}
int mode, maxTimes = 0;
for (auto it = m.begin(); it != m.end(); ++it) {
if (it->second > maxTimes || (it->second == maxTimes && it->first < mode)) {
mode = it->first;
maxTimes = it->second;
}
}
cout << mode << endl;
double accum = 0.0;
for (int i = 0; i < n; i++) {
accum += (v[i] - mean) * (v[i] - mean);
}
double stdev = sqrt(accum / (double)n);
cout << stdev << endl;
return 0;
}