Ultraviolet A 10487-Closest Sums

Source: Internet
Author: User

Closest Sums
Input: standard input
Output: standard output
Time Limit: 3 seconds

 

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 mlines 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 shoshould 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

312173334315130312331233

12334560 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. A set composed of numbers is given, and then a word is input to find out the two numbers in the Set and the value closest to the input: first, find the sum of any two numbers in the Set, sort them, and finally find them. Because the data size is relatively small, it can be compared one by one.

#include<stdio.h>#include<math.h>#include<algorithm>using namespace std;int a[1005],s[1000005],q[30];int main(){int i,j,p,n,m,k,cas=0;while(~scanf("%d",&n)&&n){for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0,k=0;i<n-1;i++)for(j=i+1;j<n;j++)s[k++]=a[i]+a[j];sort(s,s+k);scanf("%d",&m);for(i=0;i<m;i++)scanf("%d",&q[i]);printf("Case %d:\n",++cas);for(j=0;j<m;j++){if(q[j]<=s[0])p=s[0];else if(q[j]>=s[k-1])p=s[k-1];else{for(i=0;i<k;i++){if(s[i]<q[j])     continue;else{p=abs(s[i]-q[j])<abs(s[i-1]-q[j])?s[i]:s[i-1];break;}}}printf("Closest sum to %d is %d.\n",q[j],p);}}return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.