HNCU1323: 算法2-1:集合union
来源:互联网 发布:淘宝 冰毒 编辑:程序博客网 时间:2024/05/22 03:20
http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1323
题目描述
假设利用两个线性表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 OK 1#define ERROR 0#define OVERFLOW 0#define LIST_INIT_SIZE 100//线性表存储空间的初始分配量 #define LISTINCREMENT 10//线性表存储空间的分配增量 #define N 100typedef int ElemType;typedef int Status;//-----------线性表的动态分配存储结构------------ typedef struct { ElemType *elem;//存储空间基址 int length;//当前长度 int listsize;//当前分配的存储容量 }list;void Print(list l){ int j = 1; printf("%d",l.elem[0]); while( j < l.length ) { printf(" %d",l.elem[j]); j++; } printf("\n");}list InitList(list l){ //构造一个空的线性表 l.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!l.elem ) exit(OVERFLOW); l.length = 0;//存储分配长度 l.listsize = LIST_INIT_SIZE;//初始存储容量 return l;}list ListInsert(list l,int e,int j){//在线性表的第j个位置之前插入新的元素e //j的合法值为1<=j<=length+1 ElemType *newbase,*q,*p; int length; if( j < 1||j > l.length +1)//i值不合法 exit(OVERFLOW); if(l.length >= l.listsize )//当前存储空间已满,增加分配 { newbase = (ElemType*)realloc(l.elem,(l.listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase)//内存分配失败 exit(OVERFLOW); l.elem = newbase;//新基址 l.listsize += LISTINCREMENT;//增加存储容量 } p = &(l.elem[j-1]);//p为插入位置 length = l.length ; for( q = &(l.elem[length-1]); q >= p; q--) *(q+1) = *q;//插入位置及其之后的元素后移 *p = e;//插入e l.length ++;//表长增加 return l;}Status GetElem(list l,int i,int *n){ if(i < 0||i > l.length) return ERROR; else { *n = l.elem[i]; return OK; }}Status ListLength(list l){//计算长度 return l.length ;}Status Compare(int a,int b){//比较函数 if( a == b) return OK; return ERROR; }Status LocateElem(list l,int e){//找到第一个与l满足compare()数据的元素位序 int i; for( i = 0;i < l.length ; i ++) { if(Compare(l.elem[i],e)) return OK; } return ERROR;}void Union(list la,list lb){//将所有在线性表lb中,但不在la中的数据元素插入到la中 int la_len,lb_len; int e; la_len = ListLength(la);//求线性表长度 lb_len = ListLength(lb); int i,j; for( i = 0; i < lb_len; i ++) { GetElem(lb,i,&e);//取lb中第i个元素赋值给e if(!LocateElem(la,e)) la = ListInsert(la,e,++la_len); Print(la); }} int main(){ int m,n; int a[N+10],b[N+10]; int i,j; list la,lb; int t = 0; while(scanf("%d",&m)!=EOF) { la = InitList(la); lb = InitList(lb); for( i = 1; i <= m; i ++) scanf("%d",&a[i]); scanf("%d",&n); for( i = 1; i <= n; i ++) scanf("%d",&b[i]); j = 1; for( i = 1; i <= m; i ++) { la = ListInsert(la,a[i],j); j++; } Print(la); j = 1; for( i = 1; i <= n; i ++) { lb = ListInsert(lb,b[i],j); j++; } Print(lb); Union(la,lb); t++; if(t) printf("\n"); } return 0;}
阅读全文
0 0
- HNCU1323: 算法2-1:集合union
- HNCU1323:算法2-1:集合union (线性表)
- 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
- Coder Buct 2190: 算法2-1:集合union
- 【数据结构】-线性表-顺序表-1323: 算法2-1:集合union
- 率辉考研oj——1323: 算法2-1:集合union
- 算法(1):Union-Find
- 集合UNION
- MySQL中UNION集合
- 合并两个集合 union
- Linq 集合处理(Union)
- hncu1323线性表
- union union all intersect minus 集合
- Java是绑定于一种语言的跨系统的平台
- 使用git命令 (git reset --hard HEAD) 回退版本信息
- ndk开发文档
- SQLServer占CPU100%
- fasttext使用笔记
- HNCU1323: 算法2-1:集合union
- oracle 如何查看日志?
- LeetCode 405. Convert a Number to Hexadecimal (进制转换,移位运算)
- Python笔记002——错误TypeError: 'str' does not support the buffer interface
- BZOJ 1560 [JSOI2009] 火星藏宝图
- /usr/bin/ld: 找不到 -lopencv_dep_cudart
- python 列表操作 及 使用方法
- 前端开发者必备google插件
- Matlab中图形标记