forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10487.cpp
49 lines (44 loc) · 893 Bytes
/
10487.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
#include <bits/stdc++.h>
using namespace std;
int numOfInteger, numOfQuery, kase = 1, arr[1001];
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
void solve(int value)
{
int min_, record, i, j;
min_ = 0x7fffffff;
for (i = 0; i < numOfInteger - 1; i++)
for (j = i + 1; j < numOfInteger; j++)
{
int sum;
sum = arr[i] + arr[j];
if (fabs(value - sum) < min_)
{
min_ = (int)fabs(value - sum);
record = sum;
}
}
cout << "Closest sum to " << value << " is " << record << "." << endl;
}
int main()
{
while (cin >> numOfInteger && numOfInteger)
{
cout << "Case " << kase++ << ":" << endl;
int index, value;
for (index = 0; index < numOfInteger; index++)
{
cin >> arr[index];
}
qsort(arr, numOfInteger, sizeof(int), cmp);
cin >> numOfQuery;
while (numOfQuery--)
{
cin >> value;
solve(value);
}
}
return 0;
}