include iostream include unordered_map include vector include algorith

 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
double const EPS = 0.2;
bool IsEqual(double const a, double const b)
{
return std::abs(a - b) < EPS;
}
void findFrequency(const std::vector<double> & A)
{
auto A_(A);
std::vector<size_t> fq;
fq.resize(A_.size(), 0);
for (auto & x : A_)
{
std::cout << x << " ";
}
std::cout << std::endl;
std::sort(A_.begin(), A_.end());
auto len_ = A_.size();
for (size_t i = 0; i < len_; i++)
{
size_t left_go = 0;
for (size_t j = i; j >= 0; j--)
if (IsEqual(A_[i], A_[j])) left_go++; else break;
size_t right_go = 0;
for (size_t j = i; j < len_; j++)
if (IsEqual(A_[i], A_[j])) right_go++; else break;
fq[i] = left_go + right_go - 1;
}
for (auto & x : A_)
{
std::cout << x << " ";
}
std::cout << std::endl;
for (auto & x : fq)
{
std::cout << x << " ";
}
std::cout << std::endl;
}
int main()
{
// std::vector<double> A = { 4.0, 2.0, 5.0, 8.0, 4.0, 6.0, 9.0, 8.0, 5.0, 3.0, 3.0, 4.0, 1.0, 9.0, 9.0 };
std::vector<double> A = { 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5.0, 5.1, 5.2 };
findFrequency(A);
return 0;
}