HDU2034 人见人爱A-B(数组映射)
2018-03-21 17:22
239 查看
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 73 7 2 5 8 2 3 4 5 6 7 8
0 0
Sample Output
2 3NULL
solution
把第一个集合各个数字组映射到一个数组,以第二个集合元素为索引查看数组。#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int arr[999999]; int a[111], res[111]; int main() { freopen("in.txt", "r", stdin); int m, n; while (~scanf("%d%d", &m, &n) && (m || n)) { memset(arr, 0x3f, sizeof(arr)); memset(a, 0, sizeof(a)); memset(res, 0, sizeof(res)); for (int i = 0; i < m; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) { int tmp; scanf("%d", &tmp); arr[tmp] = 1; } int length_inc = 0; for (int i = 0; i < m; i++) if (arr[a[i]] != 1) res[length_inc++] = a[i]; if (length_inc == 0) { printf("NULL\n"); continue; } sort(res, res + length_inc); for (int i = 0; i < length_inc; i++) printf("%d ", res[i]); printf("\n"); } return 0; }
相关文章推荐
- hdu 2034 人见人爱A-B
- hdu-2034-人见人爱A-B
- HDU 2034 人见人爱A-B
- hdu_2034_人见人爱A-B
- HDU 2034 人见人爱A-B
- hdu 2034 人见人爱A-B (java)
- hdu 2034 人见人爱A-B
- HDU 2034 人见人爱A—B
- 杭电 HDU 2034 人见人爱A-B
- hdu-2034-人见人爱A-B
- HDU 2034 人见人爱A-B
- hdu2034 人见人爱A-B(C语言)
- hdu 2034 人见人爱A-B
- hdu2034人见人爱A-B
- HDU 2034 人见人爱A-B(坑呀...格式)
- hdu-oj 2034 人见人爱A-B
- hdu 2034 人见人爱A-B
- HDU 2034 人见人爱A-B(map)
- HDU 2034 人见人爱A-B
- HDU 2034 人见人爱A-B_0