zoukankan      html  css  js  c++  java
  • HNCU1323: 算法2-1:集合union

    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,第一个整数m0<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 100
    typedef 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("
    ");
    }
    
    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("
    ");
    
        }
        return 0;
    }
  • 相关阅读:
    linux基础命令一
    Mac安装vue cli或者electron时 npm i 报错
    记MacOS抹盘后--使用U盘安装MacOS实录
    腾讯云申请SSL证书与Nginx配置Https
    Windows Server 2016 安装虚拟机版黑群晖
    FreeNas搭建踩坑指南(三)
    FreeNas搭建踩坑指南(二)
    FreeNas搭建踩坑指南(一)
    Apache2配置多域名站点及支持https
    ubuntu server 16.04 开启root密码登录
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7350151.html
Copyright © 2011-2022 走看看