[C++] C语言计算两个有序数组中的公共元素 →→→→→进入此内容的聊天室

来自 , 2020-12-23, 写在 C++, 查看 106 次.
URL http://www.code666.cn/view/46a4378f
  1. // 找出两个数组的共同元素
  2. int* FindCommon(int* a, int* b, int nA, int nB, int& nOut)
  3. {
  4.     int i = 0;
  5.     int j = 0 ;
  6.         vector<int> vec_comm;
  7.  
  8.         int* output = NULL;
  9.        
  10.     while (i < nA && j < nB)
  11.     {
  12.         if (a[i] < b[j])
  13.             ++i ;
  14.         else if(a[i] == b[j])
  15.         {
  16.                         vec_comm.push_back(a[i]);
  17.             cout << a[i] << endl ;
  18.             ++i ;
  19.             ++j ;
  20.         }
  21.         else// a[i] > b[j]
  22.             ++j ;
  23.     }
  24.  
  25.         nOut = vec_comm.size();
  26.  
  27.         int nCoun = 0;
  28.         if (nOut>0)
  29.         {
  30.                 output=new int[nOut];
  31.                 vector<int>::iterator itorComm;
  32.  
  33.                 for(itorComm=vec_comm.begin(); itorComm!=vec_comm.end(); itorComm++)
  34.                 {
  35.                         output[nCoun] = *itorComm;
  36.                         nCoun++;
  37.                 }
  38.         }
  39.  
  40.         return output;
  41. }
  42.  
  43.  
  44. //cpp/8764

回复 "C语言计算两个有序数组中的公共元素"

这儿你可以回复上面这条便签

captcha