【题目39】整数集合的并差交集
2009-10-12 14:12
274 查看
题目:两个整数集合A,B,求其并集,交集,差集,对称差集,要求写出代码
分析: 首先对数组进行排序,如果数组无序的话。
具体可以参考侯捷《STL源码解析》的方法实现。我这里就是参照那里的方
法实现的。详情请见源码。
例子: A: 1 3 5 7 9 11
B: 1 1 2 3 5 8 13
Union of A and B: 1 1 2 3 5 7 8 9 11 13
Intersection of A and B: 1 3 5
Difference of A and B(A-B): 7 9 11
Difference of B and A(B-A): 1 2 7 8 9 11 13
Symmetric difference of A and B: 1 2 7 8 8 11 13
源码如下:
分析: 首先对数组进行排序,如果数组无序的话。
具体可以参考侯捷《STL源码解析》的方法实现。我这里就是参照那里的方
法实现的。详情请见源码。
例子: A: 1 3 5 7 9 11
B: 1 1 2 3 5 8 13
Union of A and B: 1 1 2 3 5 7 8 9 11 13
Intersection of A and B: 1 3 5
Difference of A and B(A-B): 7 9 11
Difference of B and A(B-A): 1 2 7 8 9 11 13
Symmetric difference of A and B: 1 2 7 8 8 11 13
源码如下:
#include <stdio.h> #define N 100 int result ; void set_union(int* a, int lenA,int* b,int lenB) { int i = 0, j = 0,k = 0; while(true) { if(a[i] < b[j]) { result[k++] = a[i]; i++; } else if(a[i] > b[j]) { result[k++] = b[j]; j++; } else { result[k++] = a[i]; i++; j++; } if(i >= lenA || j >= lenB) break; } while(i < lenA) { result[k++] = a[i]; i++; } while(j < lenB) { result[k++] = b[j]; j++; } } void set_intersection(int* a, int lenA,int* b,int lenB) { int i = 0, j = 0,k = 0; while(true) { if(a[i] < b[j]) { i++; } else if(a[i] > b[j]) { j++; } else { result[k++] = a[i]; i++; j++; } if(i >= lenA || j >= lenB) break; } } void set_difference(int* a, int lenA,int* b,int lenB) { int i = 0, j = 0,k = 0; while(true) { if(a[i] < b[j]) { result[k++] = a[i]; i++; } else if(a[i] > b[j]) { j++; } else { i++; j++; } if(i >= lenA || j >= lenB) break; } while(i < lenA) { result[k++] = a[i]; i++; } } void set_symmetric_difference(int* a, int lenA,int* b,int lenB) { int i = 0, j = 0,k = 0; while(true) { if(a[i] < b[j]) { result[k++] = a[i]; i++; } else if(a[i] > b[j]) { result[k++] = b[j]; j++; } else { i++; j++; } if(i >= lenA || j >= lenB) break; } while(i < lenA) { result[k++] = a[i]; i++; } while(j < lenB) { result[k++] = b[j]; j++; } } void InitArray(int *a,int n) { for(int i = 0; i < n; i++) a[i] = -1; } void PrintArray(int* a, int n) { for(int i = 0; i < n; i++) { if(a[i] == -1) break; else printf("%d ",a[i]); } printf("/n"); } int main() { //if not sorted, sort it first int A[6] = {1, 3,5,7,9,11}; int B[7] = {1,1,2,3,5,8,13}; InitArray(result,N); set_union(A,6,B,7); PrintArray(result,N); InitArray(result,N); set_intersection(A,6,B,7); PrintArray(result,N); InitArray(result,N); set_difference(A,6,B,7); PrintArray(result,N); InitArray(result,N); set_difference(B,7,A,6); PrintArray(result,N); InitArray(result,N); set_symmetric_difference(A,6,B,7); PrintArray(result,N); return 0; }
相关文章推荐
- 两个整数集合的交集 ———— 腾讯2014软件开发笔试题目
- 有两个整数集合A,B,验证A^B=B,即:A和B的交集为B
- SDUT 离散题目4 求两个集合的交集
- 一个关于把集合拆分的有意思的小题目(将1到N的连续整数组成的集合划分成两个子集合)
- 任意长度的两个整数集合求并集与交集
- 2个有序的整数集合,怎么获得交集呢?
- 求有序整数集合a和b的交集函数
- 有两个有序整数集合a和b,写一个函数找出它们的交集?
- [笔试]两个整数集合求交集并集
- 设计算法,求AB两个整数集合的交集
- 转:A、B两个整数集合,设计一个算法求他们的交集,尽可能的高效。
- SDUT 离散题目4 求两个集合的交集
- A、B两个整数集合,设计一个算法求他们的交集,尽可能的高效
- A、B两个整数集合,设计一个算法求他们的交集,尽可能的高效
- A、B两个整数集合,设计一个算法求他们的交集,尽可能的高效
- A、B两个整数集合,设计一个算法求他们的交集,尽可能的高效。
- 79 两个整数集合A和B,求其交集
- 两个整数集合A和B,求其交集
- 求两个整数集合的交集
- A,B两个整数集合,设计一个算法求他们的交集,尽可能的高效(牛客网)