程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言入門知識 >> 數據結構(線性結構習題)Problem A: 求集合的交並補集

數據結構(線性結構習題)Problem A: 求集合的交並補集

編輯:C語言入門知識

Problem A: 求集合的交並補集

Time Limit: 1 SecMemory Limit: 4 MB
Submit: 6817Solved: 1972
[Submit][Status][Web Board]

Description

任意給定兩個包含1-30000個元素的集合A,B(集合中元素類型為任意整型數,且嚴格遞增排列),求A交B、A並B、A-B和B-A集合。

Input

輸入第一行為測試數據組數。每組測試數據兩行,分別為集合A、B。每行第一個數n(1<=n<=30000)為元素數量,後面有n個嚴格遞增的絕對值小於2^31代表集合中包含的數。

Output

對每組測試數據輸出5行,第1行為數據組數,後4行分別為按升序輸出兩個集合的A交B、A並B、A-B和B-A集合。格式見樣例。

Sample Input

1
3 1 2 5
4 2 3 5 8

Sample Output

Case #1:
2 5
1 2 3 5 8
1
3 8

HINT

考察知識點:有序表合並,時間復雜度O(n),空間復雜度O(n)

解題思路:1)分析 數據/元素 需要用什麼結構儲存 2)設計算法實現

#include <stdio.h>     
#include <malloc.h>     
#include <iostream>     
#include <stdlib.h>     
#define LIST_INIT_SIZE 100     
#define LISTINCREMENT 10     
#define OK 1     
#define OVERFLOW -1     
#define ERROR 0     
    
        
typedef int Status;     
        
typedef int ElemType;     
        
typedef struct SqList{     
    ElemType * elem;   //數組首地址     
    int listSize;  //表容量;當前表的容量     
    int length;  //表長,代表當前數組中有效元素的個數     
}SqList;     
        
// 下面實現nitList操作,即初始化一個空的線性表     
Status InitList_Sq(SqList &L)     
{     
    L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));     
    //malloc的返回值類型是void *;     
    //使用時要及時進行強制類型轉換     
    if(!L.elem)     
        exit(OVERFLOW);     
    L.listSize=LIST_INIT_SIZE;     
    L.length=0;     
    return OK;     
}     
      
    
          
Status CreateList(SqList &La,int na){      
    for(int i = 1;i<=na;++i){      
        ElemType e;      
//      printf("請輸入第%d個元素",i);      
        scanf("%d",&e);      
   if(La.length >= La.listSize)   {      
        La.elem=(ElemType *)realloc(La.elem,(La.listSize+LISTINCREMENT)*sizeof(ElemType));      
        La.listSize += LISTINCREMENT;      
    }      
        La.elem[i-1]=e;      
        La.length++;      
    }      
    return OK;      
}        
  
        
void MergeList_Sq(SqList La,SqList Lb,SqList &Ld,SqList &Le)     
{  //Ld是交,Le是補   
    ElemType* pa = La.elem;     
    ElemType* pb = Lb.elem;     
  
    Ld.length = 0;     
    Ld.listSize = La.length + Lb.length;     
    Ld.elem = (ElemType*)malloc(Ld.listSize*sizeof(ElemType));     
    ElemType* pd = Ld.elem;     
    if(!Ld.elem)     
        exit(OVERFLOW);     
        
    Le.length = 0;     
    Le.listSize = La.length + Lb.length;     
    Le.elem = (ElemType*)malloc(Ld.listSize*sizeof(ElemType));     
    ElemType* pe = Le.elem;      
    if(!Le.elem)     
        exit(OVERFLOW);     
        
        
    ElemType* pa_last = La.elem + La.length -1;     
    ElemType* pb_last = Lb.elem + Lb.length -1;     
    while(pa <= pa_last && pb <= pb_last)     
    {     
        if(*pa <= *pb)     
        {     
            if(*pa == *pb)     
            {     
                *pd++ = *pa;     
                Ld.length++;     
            }     
            else  
            {     
                *pe++ = *pa;      
                Le.length++;     
            }     
           // *pc++ = *pa++;     
            pa++;   
        }     
        else  
        {     
            *pe++ = *pb;     
            Le.length++;     
          //  *pc++ = *pb++;     
            pb++;   
        }     
    }     
    while(pa <= pa_last)     
    {     
        *pe++ = *pa;     
        Le.length++;     
        //*pc++ = *pa++;     
        pa++;   
    }     
        
    while(pb <= pb_last){     
        *pe++ = *pb;     
        Le.length++;     
       // *pc++ = *pb++;    
        pb++;   
    }     
}     
        
void MergeList_Sq2(SqList La,SqList Lb,SqList &Lc)     
{     
    int i,j;     
    Lc.length = 0;     
    Lc.listSize = La.length + Lb.length;     
    Lc.elem = (ElemType*)malloc(Lc.listSize*sizeof(ElemType));     
    int n = 0;     
    for(i = 0;i < La.length;i++){     
        j = 0;     
        while((j < Lb.length)&&(La.elem[i] != Lb.elem[j])){     
            j++;     
        }     
        if(j == Lb.length){     
            Lc.elem[n] = La.elem[i];     
            ++Lc.length; ++n;    
        }     
    }     
          
}     
        
        
void ListPrint_Sq(SqList L){      
    if(L.length==0) {      
        printf("\n");      
    }      
    else  
    {      
        for(int i=0;i<L.length;++i){      
            if(i==0){      
              printf("%d",L.elem[i]);      
            }      
            else{      
              printf(" %d",L.elem[i]);      
                }      
        }      
        printf("\n");      
    }      
}      
    
        
int main()     
{     
    int num,i;     
    scanf("%d",&num);     
    for(i = 1;i <= num;i++)     
    {     
        SqList La,Lb,Ld,Le,Lf,Lg;     
        InitList_Sq(La);     
        InitList_Sq(Lb);     
    
        int na,nb;     
        scanf("%d",&na);    
        CreateList(La,na);   
        scanf("%d",&nb);     
        CreateList(Lb,nb);   
  
        MergeList_Sq(La,Lb,Ld,Le);   
        MergeList_Sq2(La,Ld,Lf);     
        MergeList_Sq2(Lb,Ld,Lg);     
        printf("Case #%d:\n",i);     
        //ListPrint_Sq(Lc);     
        ListPrint_Sq(Ld);     
        ListPrint_Sq(Le);     
        ListPrint_Sq(Lf);     
        ListPrint_Sq(Lg);     
        
    }     
        
    return 0;     
}
    No
  1. 下一頁:
Copyright © 程式師世界 All Rights Reserved