1323:算法2-1:集合union
来源:互联网 发布:北京知象科技 编辑:程序博客网 时间:2024/05/21 22:47
题目描述
假设利用两个线性表LA和LB分别表示两个集合A和B(即:线性表中的数据元素即为集合中的成员),现要求一个新的集合A=A∪B。这就要求对线性表做如下操作:扩大线性表LA,将存在于线性表LB中而不存在于线性表LA中的数据元素插入到线性表LA中去。只要从线性表LB中依次取得每个元素,并依值在线性表LA中进行查访,若不存在,则插入之。上述操作过程可用下列算法描述之。
图:将两个列表合并的算法(C/C++描述)
上图算法中,在第8行取得集合B中的元素,然后再在第10行插入到集合A中。你的任务是先输出集合A和集合B中的元素,每个集合在一行中输出。然后每次在将集合B中的元素取出插入到集合A尾部后输出集合A中的元素。当然你的代码可以和上面的代码不一样,只要有相同的输出即可。
输入格式
有多组测试数据,每组测试数据占两行。第一行是集合A,第一个整数m(0<m<=100)代表集合A起始有m个元素,后面有m个整数,代表A中的元素。第二行是集合B,第一个整数n(0<n<=100)代表集合B起始有n个元素,后面有n个整数,代表B中的元素。每行中整数之间用一个空格隔开。
输出
每组测试数据输出n+2行:前两行分别输出集合A、集合B中的数据,后面n行是每次从B中取出元素插入到A尾部后的集合A。每行整数之间用一个空格隔开,每组测试数据之间用一行空行隔开。
样例输入
5 1 5 2 6 3
3 1 7 9
1 3
2 2 7
4 2 5 1 4
4 1 2 4 5
样例输出
1 5 2 6 3
1 7 9
1 5 2 6 3
1 5 2 6 3 7
1 5 2 6 3 7 9
3
2 7
3 2
3 2 7
2 5 1 4
1 2 4 5
2 5 1 4
2 5 1 4
2 5 1 4
2 5 1 4
提示[+] #include "stdio.h"#include "stdlib.h"#define List_Size 200struct data{int num;};typedef struct{struct data num[List_Size];int last;} SqList;SqList *Create_List(SqList *L){ L = (SqList *) malloc (sizeof(SqList)); L ->last = -1; return L;}void Get_Data(SqList *L,int i,int *e){*e=L->num[i].num;}void Insert_List(SqList *L){ struct data d; if(L->last == List_Size-1) exit(0); else { scanf("%d",&d.num); L->last++; L ->num[L->last].num = d.num; } }void Insert(SqList *L,int e){ if(L->last == List_Size-1) exit(0); else { L->last++; L ->num[L->last].num = e; } }int Locat_Data(SqList *L,int e){int i;for(i=0;i<=L->last;i++){if(L->num[i].num==e)return 1;}return 0;}void Show_List(SqList *L){int i;for(i=0;i<=L->last;i++) if(i==L->last) printf("%d",L->num[i].num); else printf("%d ",L->num[i].num); printf("\n");} void Fun_Union(SqList *La,SqList *Lb) {int i,e;for(i=0;i<=Lb->last;i++){ Get_Data(Lb,i,&e); if(!Locat_Data(La,e)) Insert(La,e); Show_List(La); }}int main(){ int m,n,i,flag; while(scanf("%d",&m)!=EOF) {SqList *La,*Lb; La = Create_List(La); Lb = Create_List(Lb); for(i=0;i<m;i++) Insert_List(La); scanf("%d",&n); for(i=0;i<n;i++) Insert_List(Lb); Show_List(La); Show_List(Lb); Fun_Union(La,Lb); printf("\n"); }return 0;}
#include "stdio.h"#include "stdlib.h"#define List_Size 200struct data{int num;};typedef struct{struct data num[List_Size];int last;} SqList;SqList *Create_List(SqList *L){ L = (SqList *) malloc (sizeof(SqList)); L ->last = -1; return L;}void Get_Data(SqList *L,int i,int *e){*e=L->num[i].num;}void Insert_List(SqList *L){ struct data d; if(L->last == List_Size-1) exit(0); else { scanf("%d",&d.num); L->last++; L ->num[L->last].num = d.num; } }void Insert(SqList *L,int e){ if(L->last == List_Size-1) exit(0); else { L->last++; L ->num[L->last].num = e; } }int Locat_Data(SqList *L,int e){int i;for(i=0;i<=L->last;i++){if(L->num[i].num==e)return 1;}return 0;}void Show_List(SqList *L){int i;for(i=0;i<=L->last;i++) if(i==L->last) printf("%d",L->num[i].num); else printf("%d ",L->num[i].num); printf("\n");} void Fun_Union(SqList *La,SqList *Lb) {int i,e;for(i=0;i<=Lb->last;i++){ Get_Data(Lb,i,&e); if(!Locat_Data(La,e)) Insert(La,e); Show_List(La); }}int main(){ int m,n,i,flag; while(scanf("%d",&m)!=EOF) {SqList *La,*Lb; La = Create_List(La); Lb = Create_List(Lb); for(i=0;i<m;i++) Insert_List(La); scanf("%d",&n); for(i=0;i<n;i++) Insert_List(Lb); Show_List(La); Show_List(Lb); Fun_Union(La,Lb); printf("\n"); }return 0;}
0 0
- 1323:算法2-1:集合union
- 1323:算法2-1:集合union
- 1323: 算法2-1:集合union
- 1323: 算法2-1:集合union
- 1323: 算法2-1:集合union
- Codeup_1323_算法2-1:集合union
- HNCU1323: 算法2-1:集合union
- HNCU1323:算法2-1:集合union (线性表)
- Coder Buct 2190: 算法2-1:集合union
- 【数据结构】-线性表-顺序表-1323: 算法2-1:集合union
- 率辉考研oj——1323: 算法2-1:集合union
- 算法(1):Union-Find
- 集合UNION
- MySQL中UNION集合
- 合并两个集合 union
- Linq 集合处理(Union)
- union union all intersect minus 集合
- 集合运算(UNION 与UNION ALL)
- 上转型对象
- Android开发,Eclipse未自动创建MainActivity及main.xml?
- MyEclipse6.5安装SVN插件的三种方法
- 读取本地文件夹下的图片文件名,进行比较排序
- centos上安装GCC
- 1323:算法2-1:集合union
- Firefox关闭缓存方法
- Objective-C开发之----NSData 与 NSString,Byte数组,UIImage 的相互转换
- Git 诞生记
- python获取变量的数据类型
- 总结java面试题
- Spirent TestCenter查看发出的报文
- 网站域名的选择与用户体验的差别
- 程序那些事:日志记录的作用和方法----如何在程序中记录日志