HTML5有序数列嵌套三层,三个有序数组的公共元素

#include

#include

/*

求三个有序数组的公共元素。

*/

/*

思路:

由于数组由小到大存储,

故若arr1[now1]

则arr1在now1之前的项也不可能等于arr2[now2],

故可将now1+1。

反之同理。

*/

int main(){

int arr1[] = {0,2,5,6,9};

int arr2[] = {2,4,5,7,8};

int arr3[] = {0,2,3,4,5,7,9};

int len1 = 5, len2 = 5, len3 = 7;

for (int now1 = 0, now2 = 0, now3 = 0; (now1 < len1) && (now2 < len2) && (now3 < len3);){

if (arr1[now1] < arr2[now2]){

if (arr2[now2] < arr3[now3]){

++now1;

++now2;

}

else if (arr2[now2] > arr3[now3]){

++now1;

++now3;

}

else

++now1;

}

else if (arr1[now1] > arr2[now2]){

if (arr1[now1] < arr3[now3]){

++now2;

++now3;

}

else if (arr1[now1] > arr3[now3]){

++now2;

++now3;

}

else

++now2;

}

else if (arr1[now1] == arr2[now2]){

if (arr1[now1] > arr3[now3])

++now3;

else if (arr1[now1] < arr3[now3]){

++now1;

++now2;

}

else{

printf("%d ",arr1[now1]);

++now1;

++now2;

++now3;

}

}

}

printf("\n");

system("pause");

return 0;

}

原文:http://www.cnblogs.com/mutaohengheng/p/5108040.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值