C language merges two arrays and removes duplicates

From , 5 Years ago, written in C++, viewed 196 times.
URL https://pastebin.vip/view/77431ca7
  1. // 合并两个含有nA、nB个元素的有序数组
  2. void Merge(int *a, int *b, int *c, int nA, int nB, int& nCout)
  3. {
  4.     int i = 0 ;
  5.     int j = 0 ;
  6.     int k = 0 ;
  7.        
  8.     while (i < nA && j < nB)
  9.     {
  10.         if (a[i] < b[j])// 如果a的元素小,则插入a中元素到c
  11.         {
  12.             c[k++] = a[i] ;
  13.             ++i ;
  14.                         nCout++;
  15.         }
  16.         else if (a[i] == b[j])// 如果a和b元素相等,则插入二者皆可,这里插入a
  17.         {
  18.             c[k++] = a[i] ;
  19.             ++i ;
  20.             ++j ;
  21.                         nCout++;
  22.         }
  23.         else // a[i] > b[j] // 如果b中元素小,则插入b中元素到c
  24.         {
  25.             c[k++] = b[j] ;
  26.             ++j ;
  27.                         nCout++;
  28.         }
  29.     }
  30.        
  31.     if (i == nA) // 若a遍历完毕,处理b中剩下的元素
  32.     {
  33.         for (int m = j; m < nB; ++m)
  34.                 {
  35.             c[k++] = b[m] ;
  36.                         nCout++;
  37.                 }
  38.     }
  39.     else//j == n, 若b遍历完毕,处理a中剩下的元素
  40.     {
  41.         for (int m = i; m < nA; ++m)
  42.                 {
  43.             c[k++] = a[m] ;
  44.                         nCout++;
  45.                 }
  46.     }
  47. }
  48.  
  49.  
  50. //cpp/8765

Reply to "C language merges two arrays and removes duplicates"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website