-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumm.cpp
More file actions
36 lines (34 loc) · 845 Bytes
/
summ.cpp
File metadata and controls
36 lines (34 loc) · 845 Bytes
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
double sum1(std::vector& v)
{
if (v.empty()) {
return 0.0;
}
for(size_t i = 0; i < v.size() - 1; ++i) {
std::sort(v.begin()+i, v.end());
v[i+1] += v[i];
}
return v.back();
}
double sum2(std::vector& v)
{
if (v.empty()) {
return 0.0;
}
for(size_t i = 0; i < v.size() - 1; ++i) {
std::partial_sort(v.begin() + i, v.begin() + i + 2, v.end());
v[i+1] += v[i];
}
return v.back();
}
double sum3(std::vector& v)
{
std::multiset set(v.begin(), v.end());
while (set.size() > 1) {
std::multiset::const_iterator itA = set.begin();
std::multiset::const_iterator itB = ++set.begin();
double c = *itA + *itB;
set.erase(itA, itB);
set.insert(c);
}
return !set.empty() ? *set.begin() : 0.0;
}