C程序实现对两个数组进行交集操作

c程序实现对两个数组进行交集操作

交集运算

如果数组 1 = { 1,2,3,4,6}

  数组 2 = {1,2,5,6,7 }

那么,数组1和数组2的交集是

Array1 ^ array 2 = {1,2,3,4,6} ^ {1,2,5,6,7}                 = {1,2,6}

登录后复制

一组共同的元素被称为交集。

交集的逻辑如下 −

k=0;for(i=0;i

程序

以下是执行两个数组交集操作的C程序 −

 演示

#includeint removerepeated(int size,int a[]);void sort(int size,int a[]);main(){   int i,size1,size2,size,j=0,k,intersectionsize;   printf("Enter size of an array1

");   scanf("%d",&size1);   printf("Enter size of an array2

");   scanf("%d",&size2);   int a[size1],b[size2],uni[size1+size2];   if(size1size2){      intersectionsize=size2;   }else{      intersectionsize=size1;   }   int intersection[intersectionsize];   printf("Enter numbers for array 1

");   for(i=0;i

");   for(i=0;i

");   if(size>0){      for(i=0;i

",intersection[i]);      }   }else{      printf("No intersection

");   }}int removerepeated(int size,int a[]){   int i,j,k;   for(i=0;ia[j]){            temp=a[i];            a[i]=a[j];            a[j]=temp;         }      }   }}

登录后复制

输出

当上述程序被执行时,它产生以下结果 −

Enter size of an array15Enter size of an array22Enter numbers for array 145678Enter numbers for array 241Array after intersection4

登录后复制

以上就是C程序实现对两个数组进行交集操作的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2580647.html

(0)
上一篇 2025年3月6日 13:47:53
下一篇 2025年3月6日 13:47:58

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论