您的位置:首页 > 编程语言 > C语言/C++

两个有序数组归并为一个有序数组(去重)

2013-09-25 00:30 267 查看
#include "stdafx.h"

#include "stdlib.h"

#include "string.h"

void Adjust(int a[], int m, int b[], int n)

{

 int c[100];

    int i = 0;

 int j = 0;

 int k =0;

 while ((i != m) && (j != n))

 {

  if (a[i] == b[j])

  {

   c[k++] = a[i];

   i++;

   j++;

  }

  else

  {

   while (a[i] < b[j])

   {

    c[k++] = a[i];

    i++;

   }

   while(a[i] > b[j])

   {

    c[k++] = b[j];

    j++;

   }

  }

 }

 while (i != m)

 {

  c[k++] = a[i];

  i++;

 }

 while (j != n)

 {

  c[k++] =  b[j];

  j++;

 }

 for (int i = 0; i < k; i++)

 {

  printf("%d\r\n", c[i]);

 }

 return;

}

int _tmain(int argc, _TCHAR* argv[])

{

 int a[6] = {1,3,4,7,9,10};

 int b[7] = {2,3,4,5,7,8,9};

 Adjust(a,6,b,7);

 system("pause");

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 算法