zoj3023 Equal Total Scores
2015-07-27 18:24
363 查看
Equal Total Scores
Submit Status
Description
Taro and Hanako have numbers of cards in their hands. Each of the cards has a score on it. Taro and Hanako wish to make the total scores of their cards equal by exchanging one card in one's hand with one card in the other's hand. Which of the cards should
be exchanged with which?
Note that they have to exchange their cards even if they already have cards of the same total score.
n m
s1
s2
...
sn
sn+1
sn+2
...
sn+m
The first line of a dataset contains two numbers n and m delimited by a space, where n is the number of cards that Taro has and m is
the number of cards that Hanako has. The subsequent n+m lines list the score for each of the cards, one score per line. The first n scores (froms1 up
to sn) are the scores of Taro's cards and the remaining m scores (from sn+1 up to sn+m)
are Hanako's.
The numbers n and m are positive integers no greater than 100. Each score is a non-negative integer no greater than 100.
The end of the input is indicated by a line containing two zeros delimited by a single space.
way to exchange a pair of cards that makes the total scores equal, output a pair of scores whose sum is the smallest.
In case no exchange can make the total scores equal, output a single line containing solely -1. The output must not contain any superfluous characters that do not conform to the format.
Source
Asia 2008, Aizu (Japan), Japan Domestic
分析:
水题。
ac代码:
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
int n,m,i,j;
int a[105],b[105];
int s1,s2,s;
//int x,y;
while(scanf("%d%d",&n,&m)&&n&&m)
{
s1=s2=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
s1+=a[i];
}
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
s2+=b[i];
}
if((s1+s2)%2)
{
printf("-1\n");
continue;
}
sort(a,a+n);
sort(b,b+m);
s=(s1+s2)/2;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(s1-a[i]+b[j]==s)
{
goto xx;
}
}
}
if(i!=n)
{
xx: printf("%d %d\n",a[i],b[j]);
}
else
{
printf("-1\n");
}
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
Taro and Hanako have numbers of cards in their hands. Each of the cards has a score on it. Taro and Hanako wish to make the total scores of their cards equal by exchanging one card in one's hand with one card in the other's hand. Which of the cards should
be exchanged with which?
Note that they have to exchange their cards even if they already have cards of the same total score.
Input
The input consists of a number of datasets. Each dataset is formatted as follows.n m
s1
s2
...
sn
sn+1
sn+2
...
sn+m
The first line of a dataset contains two numbers n and m delimited by a space, where n is the number of cards that Taro has and m is
the number of cards that Hanako has. The subsequent n+m lines list the score for each of the cards, one score per line. The first n scores (froms1 up
to sn) are the scores of Taro's cards and the remaining m scores (from sn+1 up to sn+m)
are Hanako's.
The numbers n and m are positive integers no greater than 100. Each score is a non-negative integer no greater than 100.
The end of the input is indicated by a line containing two zeros delimited by a single space.
Output
For each dataset, output a single line containing two numbers delimited by a single space, where the first number is the score of the card Taro gives to Hanako and the second number is the score of the card Hanako gives to Taro. If there is more than oneway to exchange a pair of cards that makes the total scores equal, output a pair of scores whose sum is the smallest.
In case no exchange can make the total scores equal, output a single line containing solely -1. The output must not contain any superfluous characters that do not conform to the format.
Sample Input
2 2 1 5 3 7 6 5 3 9 5 2 3 3 12 2 7 3 5 4 5 10 0 3 8 1 9 6 0 6 7 4 1 1 2 1 2 1 4 2 3 4 3 2 3 1 1 2 2 2 0 0
Output for the Sample Input
1 3 3 5 -1 2 2 -1
Source
Asia 2008, Aizu (Japan), Japan Domestic
分析:
水题。
ac代码:
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
int n,m,i,j;
int a[105],b[105];
int s1,s2,s;
//int x,y;
while(scanf("%d%d",&n,&m)&&n&&m)
{
s1=s2=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
s1+=a[i];
}
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
s2+=b[i];
}
if((s1+s2)%2)
{
printf("-1\n");
continue;
}
sort(a,a+n);
sort(b,b+m);
s=(s1+s2)/2;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(s1-a[i]+b[j]==s)
{
goto xx;
}
}
}
if(i!=n)
{
xx: printf("%d %d\n",a[i],b[j]);
}
else
{
printf("-1\n");
}
}
return 0;
}
相关文章推荐
- iOS 创建静态库static library
- hdu 2955 Robberies解题报告
- 在heroku上部署gost代理服务端
- 【开源】OSharp3.0框架解说系列(6.2):操作日志与数据日志
- java基础第一讲——JDK、注释、帮助文档、编码规范
- linux下部署多台tomcat
- hdu 1671 Phone List(Trie树)
- C++读取Excel的XLS文件的方法
- 图片旋转,拖拽,缩放,删除一体
- 010--maven项目与myEclipse
- const在基本数据类型和指针类型中的用法小结
- 从join on和where执行顺序认识T-SQL查询处理执行顺序
- 树形DP入门总结
- 美团Android自动化之旅—适配渠道包
- jquery checkbox勾选/取消勾选的诡异问题
- Balanced Numbers(数位+状压)
- 线程池的创建以及使用
- class
- 名字的漂亮度
- JVM 进阶五