VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 3955|回复: 1

07年北电笔试题——两个基本有序数组的合并

[复制链接]
 楼主| 发表于 2008-10-6 14:40:11 | 显示全部楼层 |阅读模式

  1. #include <stdio.h>
  2. int merge_set(int a[],int la, int b[], int lb,int c[])
  3. {
  4. int i=0;
  5. int j=0;
  6. int k=0;
  7. int tmp;
  8. while(i<la && j<lb)
  9. {
  10.   if(a[i] < b[j])
  11.    tmp = a[i++];
  12.   else
  13.    tmp = b[j++];
  14.   if (k==0)
  15.   {
  16.    c[k++] = tmp;
  17.   }
  18.   else if (tmp != c[k - 1])
  19.   {
  20.    c[k++] = tmp;
  21.   }
  22. }
  23. while (i < la)
  24. {
  25.   if (k==0 || a[i]!=c[k-1] )
  26.   {
  27.    c[k]=a[i];
  28.    k++;
  29.   }
  30.   i++;
  31. }

  32. while (j<lb)
  33. {
  34.   if ( k==0 || b[j]!=c[k-1] )
  35.   {
  36.    c[k]=b[j];
  37.    k++;
  38.   }
  39.   j++;
  40. }
  41. return k;
  42. }
  43. void print(int a[], int la)
  44. {
  45. int i;
  46. for(i=0;i<la;i++)
  47. {
  48.   printf("%d ",a[i]);
  49. }
  50. printf("\n");
  51. }
  52. void main()
  53. {
  54. int a[6]={3,5,8,8,11,11};
  55. int b[12]={2,3,6,8,11,11,11,15,15,20,23,26};
  56. const int la=6;
  57. const int lb=12;
  58. int c[la+lb];
  59. int lc;
  60. lc=merge_set(a,la,b,lb,c);
  61. print(c,lc);
  62. printf("\nlength is %d\n",lc);
  63. }

复制代码

TC++ 3.0 上调试通过。
(XXD,其实以前考二级C语言时就有这题了,这些xx就会整天互相抄袭一些老题,俗)

[ 本帖最后由 VBProFan 于 2008-10-6 14:41 编辑 ]
发表于 2008-11-12 21:12:19 | 显示全部楼层
原帖由 VBProFan 于 2008-10-6 14:40 发表

(XXD,其实以前考二级C语言时就有这题了,这些xx就会整天互相抄袭一些老题,俗)

...


同感。。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2023-3-22 04:43

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表