Data structure and Algorithm analysis: Calculation of two ordered table intersection and set

Source: Internet
Author: User
Tags printf

"Data structure and algorithm analysis, C language description" table, stacks and queues part of the after-school exercises.

Just a test, record.

OutPut:

Array 4 5 7 no.1:2  
no.2:3 4 7  
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  
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; }

This article is from the "About:blank h4cking" blog, please be sure to keep this source http://pnig0s1992.blog.51cto.com/393390/813949

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.