从两个已排序的数组中打印出不常见的元素

从两个已排序的数组中打印出不常见的元素

给定两个已排序的数组,输出应显示它们的非公共元素

Given : array1[]= {1, 4, 6, 9, 12}   array2[]= {2, 4, 7, 8, 9, 10}Output : 1 2 6 7 8 10 12

算法

STARTStep 1 -> declare two arrays array1 and array2 with elements as int and variables n1, n2, i to 0 and j to 0Step 2 -> calculate number of elements in array1 sizeof(array1)/sizeof(array1[0])Step 3-> calculate number of elements in array2 sizeof(array2)/sizeof(array2[0])Step 4 -> Loop While till i<n1 and j<n2   IF array1[i] array2[j]         Print array2[j++ ]      End ELSE IF   ELSE      i++ and j++   End ELSEStep 5 -> End Loop WhileStep 6 -> loop While i  End Loop WhileStep 8 -> loop While j  End Loop WhileSTOP

Example

的中文翻译为:

示例

#include int main(int argc, char const *argv[]) {   int array1[]= {1, 4, 6, 9, 12};   int array2[]= {2, 4, 7, 8, 9, 10};   int n1, n2, i=0, j=0;   n1 = sizeof(array1)/sizeof(array1[0]); //Calculating number of elements in array1   n2 = sizeof(array2)/sizeof(array2[0]); //Calculating number of elements in array2   while(i < n1 && j < n2) {      if(array1[i] <array2[j]) //checking whether the element of array1 is smaller than array2         printf("%d

", array1[i++]); else if (array1[i] > array2[j]) //checking whether the element of array2 is smaller than array1 printf("%d

", array2[j++]); else { //if they are equal increment both i and j i++; j++; } } while(i < n1 && array1[i]!=array2[j]) //print remaining array1 printf("%d

", array1[i++]); while(j < n2 && array2[j]!=array1[i]) //print remaining array1 printf("%d

", array2[j++]); return 0;}

输出

如果我们运行上面的程序,它将生成以下输出

126781012

以上就是从两个已排序的数组中打印出不常见的元素的详细内容,更多请关注创想鸟其它相关文章!

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 22:07:23
下一篇 2025年12月17日 22:07:35

发表回复

登录后才能评论
关注微信