-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10487.cpp
More file actions
47 lines (43 loc) · 975 Bytes
/
Copy path10487.cpp
File metadata and controls
47 lines (43 loc) · 975 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
37
38
39
40
41
42
43
44
45
46
47
// Author: btjanaka (Bryon Tjanaka)
// Problem: (UVa) 10487
#include <bits/stdc++.h>
using namespace std;
int arr[10000];
int sums[1001000];
int find_closest(int n, int q) {
int min_diff = 1000000000;
int closest = -1;
int diff;
for (int i = 0; i < n; ++i) {
diff = abs(sums[i] - q);
if (diff < min_diff) {
min_diff = diff;
closest = sums[i];
}
}
return closest;
}
int main() {
int n;
for (int i = 1; scanf("%d", &n) > 0 && n > 0; ++i) {
// Receive input and sort
for (int j = 0; j < n; ++j) scanf("%d", &arr[j]);
int l = 0;
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
sums[l++] = arr[j] + arr[k];
}
}
sort(sums, sums + l);
// Process queries
int m;
scanf("%d", &m);
int q;
printf("Case %d:\n", i);
for (int j = 0; j < m; ++j) {
scanf("%d", &q);
printf("Closest sum to %d is %d.\n", q, find_closest(l, q));
}
}
return 0;
}