标签:
#include <stdio.h> #include <stdlib.h> /* 求三个有序数组的公共元素。 */ /* 思路: 由于数组由小到大存储, 故若arr1[now1]<arr2[now2], 则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