Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1428 

Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number. Input Input contains multiple cases. Each case starts with an integer n (1 < n ≤ 1000), which indicates, how many numbers are in the set of integer. Next n lines contain n numbers. Of course there is only one number in a single line. The next line contains a positive integer m giving the number of queries, 0 < m < 25. The next m lines contain an integer of the query, one per line. Input is terminated by a case whose n = 0. Surely, this case needs no processing. Output Output should be organized as in the sample below. For each query output one line giving the query value and the closest sum in the format as in the sample. Inputs will be such that no ties will occur.

Sample Input 5 3 12 17 33 34 3 1 51 30 3 1 2 3 3 1 2 3 3 1 2 3 3 4 5 6 0

Sample Output Case 1: Closest sum to 1 is 15.

Closest sum to 51 is 51.

Closest sum to 30 is 29.

Case 2: Closest sum to 1 is 3.

Closest sum to 2 is 3.

Closest sum to 3 is 3.

Case 3: Closest sum to 4 is 4.

Closest sum to 5 is 5.

Closest sum to 6 is 5.

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n,m,v;
    int cases = 1;
    while(scanf("%d",&n),n){
        vector<int> vals;
        while(n--){
            cin >> v;
            vals.push_back(v);
        }
        vector<int> sum;
        for(int i=0;i < vals.size();i++)
            for(int j=i+1;j < vals.size();j++)
                sum.push_back(vals[i]+vals[j]);
        sort(sum.begin(),sum.end());
        cin >> m;
        printf("Case %d:\n",cases++);
        while(m--){
            cin >> v;
            auto upper = lower_bound(sum.begin(),sum.end(),v);
            auto lower = lower_bound(sum.begin(),sum.end(),v);
            if(lower != sum.begin()) --lower;
            int res;
            if(upper==sum.end()) res = *lower;
            else
                res = abs(v-*lower) < abs(v-*upper) ? *lower : *upper;
            printf("Closest sum to %d is %d.\n",v,res>;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
3
12
17
33
34
3
1
51
30
3
1
2
3
3
1
2
3
3
1
2
3
3
4
5
6
0

Output

x
+
cmd
Case 1: Closest sum to 1 is 15.
Closest sum to 51 is 51.
Closest sum to 30 is 29.
Case 2:
Closest sum to 1 is 3.
Closest sum to 2 is 3.
Closest sum to 3 is 3.
Case 3:
Closest sum to 4 is 4.
Closest sum to 5 is 5.
Closest sum to 6 is 5.
Advertisements

Demonstration


UVA Online Judge solution - 10487-Closest Sums - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 10482-The Candyman Can - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10491-Cows and Cars - UVA Online Judge solution in C,C++,java