当前位置: 代码迷 >> 综合 >> ACM Plan UVa - 10487 Closest Sums
  详细解决方案

ACM Plan UVa - 10487 Closest Sums

热度:100   发布时间:2023-10-15 12:39:57.0
Talk is cheap. Show me the code. —— Linus Torvalds
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAX = 1010;
const ll INF = 9999999999;
int n, a[MAX];
void deal(int num)
{
    ll ans, b, d;ll c = INF;for(int i = 0; i < n - 1; i++)for(int j = i + 1; j < n; j++){
    d = a[i] + a[j];if((b = abs(d - num)) < c){
    c = b;ans = d;}}printf("Closest sum to %d is %lld.\n", num, ans);
}
int main()
{
    //freopen("i.txt", "r", stdin);int m, num, t = 1;while(scanf("%d", &n), n){
    printf("Case %d:\n", t++);for(int i = 0; i < n; i++) scanf("%d", &a[i]);scanf("%d", &m);for(int i = 0; i < m; i++){
    scanf("%d", &num);deal(num);}}return 0;
}