-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexactSum.cpp
60 lines (52 loc) · 1.45 KB
/
exactSum.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
54
55
56
57
58
59
60
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <climits>
using namespace std;
ifstream fin("exactSum.in");
ofstream fout("exactSum.out");
int main()
{
while (true)
{
int n = -1, totalCost;
fin >> n;
if (n == -1)
{
break;
}
vector<int> books(n);
for (int i = 0; i <= n - 1; ++i)
{
fin >> books[i];
}
sort(books.begin(), books.end());
fin >> totalCost;
int minDifferent = INT_MAX;
int a = books[0], b = books.back();
for (int i = 0; i <= n - 1; ++i)
{
auto it = lower_bound(books.begin(), books.end(), totalCost - books[i]);
if (*it == totalCost - books[i])
{
if (it - books.begin() != i)
{
if (abs(totalCost - books[i] - books[i]) < minDifferent)
{
a = min(totalCost - books[i], books[i]);
b = max(totalCost - books[i], books[i]);
minDifferent = totalCost - books[i] - books[i];
}
}
}
}
fout << "Peter should buy books whose prices are " << a << " and " << b << ".\n\n";
}
}