#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, m, i, j, k = 0;
while(cin >> n && n > 0)
{
k++;
int a[n];
for(i=0; i<n; i++)
{
cin >> a[i];
}
sort(a, a+n);
cin >> m;
int b[m];
for(i=0; i<m; i++)
cin >> b[i];
vector<int>c;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
int t = a[i] + a[j];
c.push_back(t);
}
}
int r = 0;
cout << "Case " << k << ":" << endl;
for(i=0;i<m;i++)
{
int t = b[i];
long int mn = 100000000;
for(j=0;j<c.size();j++)
{
int p = t - c[j];
if(p < 0)
p = p * (-1);
if(mn > p)
{
mn = p;
r = c[j];
}
}
cout << "Closest sum to " << b[i] << " is " << r << "." << endl;
}
}
return 0;
}
using namespace std;
int main()
{
int n, m, i, j, k = 0;
while(cin >> n && n > 0)
{
k++;
int a[n];
for(i=0; i<n; i++)
{
cin >> a[i];
}
sort(a, a+n);
cin >> m;
int b[m];
for(i=0; i<m; i++)
cin >> b[i];
vector<int>c;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
int t = a[i] + a[j];
c.push_back(t);
}
}
int r = 0;
cout << "Case " << k << ":" << endl;
for(i=0;i<m;i++)
{
int t = b[i];
long int mn = 100000000;
for(j=0;j<c.size();j++)
{
int p = t - c[j];
if(p < 0)
p = p * (-1);
if(mn > p)
{
mn = p;
r = c[j];
}
}
cout << "Closest sum to " << b[i] << " is " << r << "." << endl;
}
}
return 0;
}
Comments
Post a Comment