計算兩個數組的交集,並去掉重複的元素,把最後結果存入其中的一個數組中。

幫別人寫得作業,沒事發上來。

/**************************************************************
 * File Name     :  Intersection.c
 * Created Date  :  2010-9-23
 * Description   :  Calculate intersection.
 * Author        :  Jean
 * Email         :  [email protected]
 * QQqun         :  122594105
 * Copyright (c) Jean. 2010-2015. All Rights Reserved.
**************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>

typedef struct List
{
 int *elem;
 int length;
 int listsize;
}SqList;

/**
 * Brief:計算La、Lb指向的數組的交集,並用Lc指向的數組
 * 來臨時地存放交集,之後再把結果存入La指向的數組中。
 *
 * 說明:函數名稱CalcIntersection爲Calculate intersection
 * 的縮寫,即“計算交集”。
 *
 * 算法設計:取出較短的數組中的每個值,用它和較長的數組中的值依次比較,
 * 如果有相等的,就把該值存入tempArray中.注意:此時在tempArray中存放的
 * 值可能有重複的,稍後我們會在把tempArray中的值copy給arrayOne時消除其
 * 中重複的元素。
 */
void CalcIntersection(SqList* La, SqList* Lb, SqList* Lc)
{
 int i = 0;
 int j = 0;
 
 if (La->length >= Lb->length)
 {  
  // 把Lb中的每一個元素和La中的元素作比較。
  for (i = 0; i < Lb->length; i++)
  {
   for (j = 0; j < La->length; j++)
   {
    if(Lb->elem[i] == La->elem[j])
     Lc->elem[Lc->length++] = Lb->elem[i];       // 找到交集中的一個元素後把它放入Lc中,
                                                             // Lc->length既表示Lc中元素個數,又表示下一個要放入的元素的索引。
   }
  }
 }
 else
 {
  // 把La中的每一個元素和Lb中的元素作比較。
  for (i = 0; i < La->length; i++)
  {
   for (j = 0; j < Lb->length; j++)
   {
    if(La->elem[i] == Lb->elem[j])
     Lc->elem[Lc->length++] = La->elem[i];       // 找到交集中的一個元素後把它放入Lc中,
                                                             // Lc->length即表示Lc中元素個數,又表示一個下個要放入的元素的索引。
   }
  }
 }

 // 把Lc中的元素copy到La中,同時保證重複的元素只保留一個。
 La->length = 0;                              // 把La初始化爲空。

 if (Lc->length > 0)                          // 如果存在交集
 {
  La->elem[La->length] = Lc->elem[0];      // 首先把Lc中的第一個元素copy到La中。
  (La->length)++;

  for (i = 1; i < Lc->length; i++)
  {
   if(Lc->elem[i] > Lc->elem[i-1])
    La->elem[(La->length)++] = Lc->elem[i];
  }
 }
}
int main()
{
 SqList arrayOne;                        // The first array to be calculated.
 SqList arrayTwo;                        // The second array to be calculated.
    // A temporary array to store the intersection between arrayOne and arrayTwo.
 // Then we'll copy it to arrayOne.
 SqList temArray;
 // Index.
  int i = 0;
 // Define two int array for testing.
 int myArrayOne[11]  = {2, 4, 6, 8, 8, 12, 14, 14, 18, 20, -1};
 int myArrayTwo[10]   = {3, 4, 4, 8, 18, 18, -1, -1, -1, -1};
 
 // Initialize arrayOne.
 arrayOne.listsize = 11;
 arrayOne.length = 10;
 arrayOne.elem = myArrayOne;
 // Initialize arrayTwo.
 arrayTwo.listsize = 10;
 arrayTwo.length = 6;
 arrayTwo.elem = myArrayTwo;
 // Initialize temArray. make its size to be longer between arrayOne and arrayTwo
 // 說明:由於tempArray要存放兩個數組的交集,爲保證可以存放的下,我們應讓tempArray的
 // 大小等於兩個數組中的較大者。
 if(arrayOne.length >= arrayTwo.length)
 {
  temArray.elem = (int*)malloc(sizeof(int) * (arrayOne.length));
  memset(temArray.elem, -1, sizeof(int) * (arrayOne.length));          // Initialze array-value to be -1;
 }
 else
 {
  temArray.elem = (int*)malloc(sizeof(int) * (arrayTwo.length));
  memset(temArray.elem, -1, sizeof(int) * (arrayTwo.length));          // Initialze array-value to be -1;
 }
 temArray.length = 0;
 temArray.listsize = arrayTwo.length; 
 // Calculate intersection of arrayOne and arrayTwo.
 CalcIntersection(&arrayOne, &arrayTwo, &temArray);
 // Output the result to screen.
 for (i = 0; i < arrayOne.length; i++)
 {
  printf("%d/n", arrayOne.elem[i]);
 }
 return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章