`
Dev|il
  • 浏览: 122295 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

HDU2034人见人爱A-B

 
阅读更多
http://acm.hdu.edu.cn/showproblem.php?pid=2034
人见人爱A-B
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18330    Accepted Submission(s): 5104


Problem Description
参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减法运算。(当然,大家都知道集合的定义,就是同一个集合中不会有两个相同的元素,这里还是提醒大家一下)

呵呵,很简单吧?


Input
每组输入数据占1行,每行数据的开始是2个整数n(0<n<=100)和m(0<m<=100),分别表示集合A和集合B的元素个数,然后紧跟着n+m个元素,前面n个元素属于集合A,其余的属于集合B. 每个元素为不超出int范围的整数,元素之间有一个空格隔开.
如果n=0并且m=0表示输入的结束,不做处理。


Output
针对每组数据输出一行数据,表示A-B的结果,如果结果为空集合,则输出“NULL”,否则从小到大输出结果,为了简化问题,每个元素后面跟一个空格.



Sample Input
3 3 1 2 3 1 4 7
3 7 2 5 8 2 3 4 5 6 7 8
0 0


Sample Output
2 3
NULL


Author
lcy


Source
ACM程序设计期末考试(2006/06/07)


Recommend
lcy
#include <stdio.h>
#include <stdlib.h>

#define _N 105

int a[_N], b[_N], c[_N];

int binSearch(int key, int length)
{
	int high = length - 1;
	int low = 0;
	int center;
	while(low <= high)
	{
		center = (low + high) / 2;
		if(b[center] == key)
		{
			return 1;
		}else if(b[center] < key)
		{
			low = center + 1;
		}else
		{
			high = center - 1;
		}
	}
	return 0;
}
int cmp(const void *a, const void *b)
{
	return *(int *)a - *(int *)b;
}
//提示:两个集合的减法定义为:除去a集合中在b集合相同的数,剩下的数就为减法的结果 如a = {1, 2, 3}
//b = {2, 3, 4} 则 a - b = {1}
int main()
{
	int n, m, i, j;
	while(~scanf("%d%d", &n, &m))
	{
		if(!n && !m)
			break;
		for(i = 0; i < n; i++)
			scanf("%d", &a[i]);
		for(i = 0; i < m; i++)
			scanf("%d", &b[i]);
		qsort(b, m, sizeof(int), cmp);
		qsort(a, n, sizeof(int), cmp);
		for(i = 0, j = 0; i < n; i++)
		{
			if(!binSearch(a[i], m))
				c[j++] = a[i];
		}
		if(j == 0)
		{
			printf("NULL\n");
			continue;
		}
		else
		{
			for(i = 0; i < j - 1; i++)
				printf("%d ", c[i]);
		}
		printf("\n");
	}
	return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics