UVA 10487-Closest Sums(遍历水题)

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 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.     

题意:给你n个数,求出两两之和,然后m次询问,找与当前询问的数相等的数并输出,如果没有相等的就输出最相近的数。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
using namespace std;
const int inf=0x3f3f3f3f;
int a[1010],q[30];
int sum[1000010];
int main()
{
    int n,m,i,j;
    int cnt=0;
    int cas=1;
    int res;
    while(~scanf("%d",&n)) {
        if(n==0) break;
        cnt=0;
        for(i=0; i<n; i++)
            scanf("%d",&a[i]);
        for(i=0; i<n-1; i++)
            for(j=i+1; j<n; j++) {
                sum[cnt]=a[i]+a[j];
                cnt++;
            }
        sort(sum,sum+cnt);
        scanf("%d",&m);
        for(i=0; i<m; i++)
            scanf("%d",&q[i]);
        printf("Case %d:\n",cas++);
        for(i=0; i<m; i++) {
            if(q[i]<=sum[0])
                res=sum[0];
            else if(q[i]>=sum[cnt-1])
                res=sum[cnt-1];
            else {
                for(j=0; j<cnt; j++) {
                    if(sum[j]<q[i])
                        continue;
                    else {
                        if(abs(sum[j]-q[i])<abs(sum[j-1]-q[i]))
                            res=sum[j];
                        else
                            res=sum[j-1];
                        break;
                    }
                }
            }
            printf("Closest sum to %d is %d.\n",q[i],res);
        }
    }
    return 0;
}
时间: 2024-10-11 03:42:19

UVA 10487-Closest Sums(遍历水题)的相关文章

uva 10487 Closest Sums (遍历&amp;二分查找&amp;&amp;双向查找)

题目大意:先给定n个数字,现在要求算出这n个数字的两两之和保存到sum数组,然后在给定m个数,要求找到和每一个数最接近的sum[i]: 挨个计算每个属于其他数之间的sum,然后排序: 查找时有两种方法:二分查找&&双向查找:当然二分查找的效率比后者高了很多,但是都能AC. 提供一条新思路,并不一定非要用二分. 双向查找: #include<stdio.h> #include<algorithm> #include<stdlib.h> using name

UVA 10487 Closest Sums(二分)

UVA 10487 Closest Sums 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

UVA - 10487 - Closest Sums (二分求解)

传送:UVA - 10487 10487 Closest Sums 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 c

uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字.要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值. 代码: #include <stdio.h> #include <algorithm> #include <stdlib.h> using

UVA11470 Square Sums【水题】

Do you know that there are squares within a square. This might seem confusing, but take a look at this. ????Suppose you have a square grid of size 5 × 5 completely filled with integers. ????You can make three squares from the table above... well ther

UVA 11100 The Trip, 2007 水题一枚

题目链接:UVA - 11100 题意描述:n个旅行箱,形状相同,尺寸不同,尺寸小的可以放在尺寸大的旅行箱里.现在要求露在最外面的旅行箱的数量最少的同时满足一个旅行箱里放的旅行箱的数量最少.求出这样满足要求的任意一种方案. 算法分析:首先我们可以确定最少的旅行箱的数量cnt:即n个旅行箱里按照尺寸大小分类(尺寸相同的在同一类),数量最多的那一类的数量.然后把cnt看成有cnt个堆,第二个要求就是要让这cnt个堆里最大旅行箱数量最小,直接用vector处理即可. 等AC之后突然想到,三个旅行箱尺寸

UVA 11947 Cancer or Scorpio 水题

Cancer or Scorpio Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3098 Description Alice and Michael is a young couple who are planning on having their

UVA 541 Error Correction【水题】

Error Correction Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Problem Description A boolean matrix has the parity property when each row and each column has an

uva 10976 fractions again(水题)——yhx

1 #include<cstdio> 2 int a[30010],b[30010]; 3 int main() 4 { 5 int i,j,k,l,m,n,x,y,z; 6 while (scanf("%d",&k)==1) 7 { 8 n=0; 9 for (i=k+1;i<=2*k;i++) 10 if ((k*i)%(i-k)==0) 11 { 12 a[++n]=k*i/(i-k); 13 b[n]=i; 14 } 15 printf("%