CODE:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAXN 1000001
#define INF 0x3f3f3f3f
int a[MAXN], sum[MAXN*10];
int n, m, tot;
void init()
{
tot = 0;
memset(sum, 0, sizeof(sum));
memset(a, 0, sizeof(a));
}
int main()
{
int times = 0, Index;
while(scanf("%d", &n) && n)
{
init();
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i == j) continue;
sum[tot++] = a[i]+a[j];
}
}
scanf("%d", &m);
printf("Case %d:\n", ++times);
while(m--)
{
int q;
scanf("%d", &q);
int Min = INF;
for(int i = 0; i < tot; i++)
{
int dif = abs(sum[i]-q);
if(dif < Min)
{
Min = dif;
Index = i;
}
}
printf("Closest sum to %d is %d.\n", q, sum[Index]);
}
}
return 0;
}
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAXN 1000001
#define INF 0x3f3f3f3f
int a[MAXN], sum[MAXN*10];
int n, m, tot;
void init()
{
tot = 0;
memset(sum, 0, sizeof(sum));
memset(a, 0, sizeof(a));
}
int main()
{
int times = 0, Index;
while(scanf("%d", &n) && n)
{
init();
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i == j) continue;
sum[tot++] = a[i]+a[j];
}
}
scanf("%d", &m);
printf("Case %d:\n", ++times);
while(m--)
{
int q;
scanf("%d", &q);
int Min = INF;
for(int i = 0; i < tot; i++)
{
int dif = abs(sum[i]-q);
if(dif < Min)
{
Min = dif;
Index = i;
}
}
printf("Closest sum to %d is %d.\n", q, sum[Index]);
}
}
return 0;
}