UVa 10487 - Closest Sums
来源:互联网 发布:c 网络验证源码 编辑:程序博客网 时间:2024/06/05 12:43
Problem D
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 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.
Piotr Rudnicki
相加+排序
#include <stdio.h>#include <string.h>#include <math.h>int a[1000];int sum[1000000];int top;int cmp(const void *e,const void *f){ return *(int *)e-*(int *)f;}int main(){ int i,j,n,m,s,t,k,min; int tem=1; while(scanf("%d",&n)!=EOF) { if(n==0) { break; } for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } for(i=0,top=0;i<=n-1;i++) { for(j=i+1;j<=n-1;j++) { sum[top]=a[i]+a[j]; top++; } } qsort(sum,top,sizeof(sum[0]),cmp); scanf("%d",&m); printf("Case %d:\n",tem);tem++; for(i=0;i<=m-1;i++) { scanf("%d",&k); min=sum[0]; for(j=1;j<=top-1;j++) { if(abs(sum[j]-k)<=abs(min-k)) { min=sum[j]; }else { break; } } printf("Closest sum to %d is %d.\n",k,min); } } return 0;}
- UVa 10487 - Closest Sums
- UVa 10487 - Closest Sums
- uva 10487 - Closest Sums
- uva 10487 - Closest Sums
- UVa 10487 - Closest Sums
- UVa 10487 - Closest Sums
- UVa 10487 - Closest Sums
- uva 10487 - Closest Sums
- UVA 10487 - Closest Sums
- uva 10487 Closest Sums
- uva 10487 Closest Sums
- uva 10487 Closest Sums
- UVa 10487 - Closest Sums
- UVa 10487 - Closest Sums
- UVa-10487-Closest Sums
- UVA - 10487 Closest Sums
- uva-10487 - Closest Sums
- UVA - 10487 Closest Sums
- JSTL中遇到的According to TLD or attribute……异常
- Flex柱状图(包含线性图)自定义显示tooltip
- Hadoop管理员的十个最佳实践
- chapter 5.3: KMDF对象模型实现
- 简析:C++与JAVA的区别(初学者必看)
- UVa 10487 - Closest Sums
- xml做mysql的配置文件及php对配置文件信息的读取 连接数据库
- C++程序设计-第2周结构体应用
- HBase Block Cache实现机制分析
- [翻译]ACMer 2013 Daily Training- 2nd Mar for 11x&&12x
- chapter 5.4: 创建对象
- 用PHP 通过DOM方式创建一个book.xml文件
- Merkel Tree在BitTorren中的应用
- 正则表达式笔记