找出两个有序整型数组重复出现过的元素

#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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值