forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosestsums.cpp
41 lines (33 loc) · 884 Bytes
/
closestsums.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
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n;
int count = 1;
while(cin >> n) {
cout << "Case " << count << ":" << endl;
count++;
vector<int> v;
v.resize(n);
for(int i = 0; i < n; i++) {
cin >> v[i];
}
int m;
cin >> m;
for(int i = 0; i < m; i++) {
int target;
cin >> target;
int closest = v[0]+v[1];
for(int j = 0; j < n; j++) {
for(int k = j+1; k < n; k++) {
int sum = v[j] + v[k];
if(abs(sum - target) < abs(closest - target)) {
closest = sum;
}
}
}
cout << "Closest sum to " << target << " is " << closest << "." << endl;
}
}
}