-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumOfDivided.cpp
More file actions
59 lines (54 loc) · 1.25 KB
/
sumOfDivided.cpp
File metadata and controls
59 lines (54 loc) · 1.25 KB
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
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
class SumOfDivided
{
private:
static std::vector<int> primeFactorsprimeFactors(int n);
public:
static std::string sumOfDivided(std::vector<int> &lst);
};
std::string SumOfDivided::sumOfDivided(std::vector<int> &lst) {
std::map <int,int>mojmap;
std::vector<int>temp;
for(int a:lst){
temp=primeFactorsprimeFactors(a);
for(int cos:temp){
mojmap[cos]+=a;
}
}
std::string returning;
for(auto ha :mojmap)
returning+="("+std::to_string(ha.first)+" "+std::to_string(ha.second)+")";
return returning;
}
std::vector<int> SumOfDivided::primeFactorsprimeFactors(int n) {
n=std::abs(n);
std::vector<int> k;
while (n % 2 == 0)
{
k.push_back(2);
n = n/2;
}
for (int i = 3; i <= std::sqrt(n); i = i + 2)
{
while (n % i == 0)
{
k.push_back(i);
n = n/i;
}
}
if (n > 2)
k.push_back(n);
std::sort( k.begin(), k.end() );
k.erase( unique( k.begin(), k.end() ), k.end() );
return k;
}
int main()
{
std::vector<int> d1 = {15, 30, -45};
std::cout<<SumOfDivided::sumOfDivided(d1);
return 0;
}