《数据结构与算法分析,C语言描述》 表,栈与队列部分课后习题。
Just a test,记录。
outPut:
- Array No.1:2 4 5 7 10 23
- Array No.2:3 4 7 13 22 30
- Sum intersection:2
- 4 7
- Sum union section:10
- 2 3 4 5 7 10 13 22 23 30
Code:
- //Code by Pnig0s1992
- //Date:2012,3,22
- #include <stdio.h>
- #include <Windows.h>
- #define MAX_ITEM 12
- int getInterSection(int arr1[],int arr2[],int arrRc[]);
- int getUnionSection(int arr1[],int arr2[],int arrRc[]);
- int main(int argc,char ** argv)
- {
- int index = 0;
- int iArray1[6] = {2,4,5,7,10,23};
- int iArray2[6] = {3,4,7,13,22,30};
- int iResult[MAX_ITEM];
- printf("\nArray No.1:");
- for(index = 0;index< 6;index++)
- {
- printf("%d ",iArray1[index]);
- }
- printf("\nArray No.2:");
- for(index = 0;index<6;index++)
- {
- printf("%d ",iArray2[index]);
- }
- int sumInter = getInterSection(iArray1,iArray2,iResult);
- printf("\nSum intersection:%d\n",sumInter);
- for(int i=0;i<sumInter;i++)
- {
- printf("%d ",iResult[i]);
- }
- int sumUnion = getUnionSection(iArray1,iArray2,iResult);
- printf("\nSum union section:%d\n",sumUnion);
- for(int j = 0;j<sumUnion;j++)
- {
- printf("%d ",iResult[j]);
- }
- system("pause");
- return 0;
- }
- int getInterSection(int arr1[],int arr2[],int arrRc[])
- {
- int i = 0;
- int j = 0;
- int k = 0;
- while(i<6&&j<6)
- {
- if(arr1[i] == arr2[j])
- {
- arrRc[k] = arr1[i];
- i++,j++,k++;
- }
- else if(arr1[i] < arr2[j])
- i++;
- else
- j++;
- }
- return k;
- }
- int getUnionSection(int arr1[],int arr2[],int arrRc[])
- {
- int i = 0,j=0,k=0;
- while(i<6 && j<6 )
- {
- if(arr1[i] < arr2[j])
- {
- arrRc[k] = arr1[i];
- i++,k++;
- }else if(arr1[i] > arr2[j])
- {
- arrRc[k] = arr2[j];
- j++;k++;
- }else
- {
- arrRc[k] = arr1[i];
- i++,j++,k++;
- }
- }
- if(i<6)
- {
- for (;i<6;i++)
- {
- arrRc[k++] = arr1[i];
- }
- }else if(j<6)
- {
- for (;j<6;j++)
- {
- arrRc[k++] = arr2[j];
- }
- }
- return k;
- }
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/813949,如需转载请自行联系原作者