#include <stdio.h>
int min(int m, int n){
if(m > n){
return n;
}else{
return m;
}
}
int max(int m, int n){
if(m > n){
return m;
}else{
return n;
}
}
int compare(int* a, int* b, size_t aLen, size_t bLen){
size_t uiNumA = 0;
size_t uiNumB = 0;
size_t uiOriA = 0;
size_t uiTailA = 0;
size_t uiOriB = 0;
size_t uiTailB = 0;
int iLeftCritical = 0;
int iRightCritical = 0;
size_t iCount = 0;
uiNumA = aLen;
uiNumB = bLen;
printf("a-num:%d && b-num:%d\n", uiNumA, uiNumB);
iLeftCritical = max(*(a), *(b));
iRightCritical = min(*(a + uiNumA - 1), *(b + uiNumB -1));
printf("左临界:%d && 右临界:%d\n", iLeftCritical, iRightCritical);
if(iLeftCritical > iRightCritical){
printf("没有交叠!\n");
return -1;
}
//求出四个交叠临界点
if(iLeftCritical == a[0]){
uiOriA = 0;
for(int i = 0; b[i] < iLeftCritical; i++){
uiOriB = i + 1;
}
}else{
//
uiOriB = 0;
for(int i = 0; a[i] < iLeftCritical; i++){
uiOriA = i + 1;
}
}
if(iRightCritical == a[uiNumA - 1]){
uiTailA = uiNumA - 1;
for(int i = 0; b[i] <= iRightCritical; i++){
uiTailB = i;
}
}else{
uiTailB = uiNumB - 1;
for(int i = 0; a[i] <= iRightCritical; i++){
uiTailA = i;
}
}
printf("A(%d, %d)\nB(%d, %d)\n\n\n", uiOriA, uiTailA, uiOriB, uiTailB);
//比较
for (int i = uiOriA; i <= uiTailA; i++){
for(int j = uiOriB; j <= uiTailB; j++){
if(a[i] == b[j]){
printf("%d\n", a[i]);
iCount++;
}
}
}
printf(" 总计重复%d个\n", iCount);
return 0;
}
int main(int argc, char** argv){
int a[10] = {12, 14, 18, 20, 29, 33, 56, 67, 69, 88};
int b[6] = {22, 34, 67, 88, 100, 145};
compare(a, b, sizeof(a)/sizeof(a[0]), sizeof(b)/sizeof(b[0]));
return 0;
}