Equal Total Scores
来源:互联网 发布:淘宝上的淘是什么意思 编辑:程序博客网 时间:2024/04/28 03:25
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, wheren is the number of cards that Taro has andm is the number of cards that Hanako has. The subsequentn+m lines list the score for each of the cards, one score per line. The firstn scores (froms1 up tosn) are the scores of Taro's cards and the remainingm scores (fromsn+1 up tosn+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 one 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.
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
两个人的牌彼此交换一张使得两人牌的数字和相等,即使两人原先和相等也要进行交换一次
#include <iostream>using namespace std;int numn[100];int numm[100];int m,n;int main(){while(cin>>n>>m&&m!=0||n!=0){int summ=0,sumn=0,mul;for(int i=0;i<n;i++){cin>>numn[i];sumn+=numn[i];}for(int i=0;i<m;i++){cin>>numm[i];summ+=numm[i];}mul=sumn-summ;if(mul%2!=0)cout<<"-1"<<endl;else{int k=0,l=0,min=0,flag=0,sum=250;for(int i=0;i<n;i++){for(int j=0;j<m;j++)if(numn[i]-numm[j]==mul/2){flag=1;if(sum>numn[i]+numm[j]){sum=numn[i]+numm[j];k=i;l=j;}}}if(flag)cout<<numn[k]<<" "<<numm[l]<<endl;elsecout<<"-1"<<endl;}}return 0;}
- Equal Total Scores
- zoj3023 Equal Total Scores
- zoj 3023 Equal Total Scores
- ZOJ 3023 Equal Total Scores
- Order-level sales credit total () must equal 100%.
- total
- equal
- equal
- Bowling Scores
- For scores
- Rank Scores
- Rank Scores
- Rank Scores
- Rank Scores
- Rank Scores
- The Beatles - Complete Scores
- Problem 22:Names scores
- [projecteuler]Names scores
- H264(NAL简介与I帧判断)
- opencv 透视变换
- jquery置顶div的方法-转载自http://www.jb51.net脚本之家
- 改变图像的对比度和亮度
- 混合云本身不是终点
- Equal Total Scores
- 人生最悲惨的事情就是如厕的时候忘记带手纸
- 如何在Eclipse开发环境进行android应用调试
- 【算法】动态规划 最长上升子序列
- Cocoa Stream 系列2---NSInputStream的使用
- ListView部分样式属性设置
- 阿里笔试经历
- [LeetCode]Path Sum II
- windows下远程Ubuntu (利用xrdp)