Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- int main()
- {
- ll n, m;
- ll c = 1;
- while (cin >> n)
- {
- vector<ll> z1(n);
- vector<ll> z3;
- for (int i = 0; i < n; i++)
- {
- cin >> z1.at(i);
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (i != j)
- {
- ll x = z1.at(j) + z1.at(i);
- z3.push_back(x);
- }
- }
- }
- cin >> m;
- vector<ll> z2(m);
- for (int i = 0; i < m; i++)
- {
- cin >> z2.at(i);
- }
- sort(z3.begin(), z3.end());
- cout << "Case " << c << ':' << endl;
- c += 1;
- for (int i = 0; i < m; i++)
- {
- ll w = lower_bound(z3.begin(), z3.end(), z2.at(i)) - z3.begin();
- if (w >= z3.size())
- {
- w = z3.size() - 1;
- }
- if (w > 0)
- {
- if (abs(z3.at(w) - z2.at(i)) > abs(z3.at(w - 1) - z2.at(i)))
- {
- w = w - 1;
- }
- }
- cout << "Closest sum to " << z2.at(i) << " is " << z3.at(w) << '.' << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment