-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosestsums.cxx
49 lines (40 loc) · 1.18 KB
/
closestsums.cxx
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 <cstdio>
#include <cstring>
#include <cmath>
int main() {
int n;
int tcase = 1;
const int len = 1000 * (1000 + 1) / 2;
int *sums = new int[len];
while (scanf("%d", &n) == 1) {
printf("Case %d:\n", tcase);
memset(sums, 0, sizeof(int) * len);
int is[1000];
for (int i = 0; i < n; i++)
scanf("%d", &is[i]);
int m;
scanf("%d", &m);
int qs[24];
for (int i = 0; i < m; i++)
scanf("%d", &qs[i]);
int x = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
sums[x] = is[i] + is[j];
x++;
}
}
int closest[24];
for (int q = 0; q < m; q++) {
int minimum = sums[0];
for (int i = 1; i < x; i++)
if (fabs(sums[i] - qs[q]) < fabs(minimum - qs[q]))
minimum = sums[i];
closest[q] = minimum;
}
for (int q = 0; q < m; q++)
printf("Closest sum to %d is %d.\n", qs[q], closest[q]);
tcase++;
}
delete[] sums;
}