免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: forrestgang
打印 上一主题 下一主题

华为面试题(8分钟写出代码) [复制链接]

论坛徽章:
0
201 [报告]
发表于 2007-11-28 19:16 |只看该作者
9楼效率应该更好
zqc99 该用户已被删除
202 [报告]
发表于 2007-12-02 10:40 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
zqc99 该用户已被删除
203 [报告]
发表于 2007-12-02 10:43 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
zqc99 该用户已被删除
204 [报告]
发表于 2007-12-02 10:52 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
205 [报告]
发表于 2007-12-10 17:06 |只看该作者
1. 将两个数组合并,排序,求和后得到平均值ave;
排序后数组:c1<=c2<=c3...
2. 交叉排放排序后的数组至两个数组a,b:
a:c1,c3,c5...
b:c2,c4,c6...
3. 数组a求和得到aSum, 求取与均值之差(由a的生成过程可知aSum<=ave):
diff = ave-aSum;
4. 由a(0)开始与b数组相比较,找出差值最接近于diff的元素:
Min (diff-(b[n]-a[m])); #n>=m
交换b[n], a[m].

得解。

[ 本帖最后由 rockyr 于 2007-12-10 17:13 编辑 ]

论坛徽章:
0
206 [报告]
发表于 2007-12-13 16:25 |只看该作者

回复 #204 zqc99 的帖子

嘿嘿   你以为就那么简单?
a  142 3 2 -12
b  31  4 2 1
这是最优解??

那你看看最优的吧
a  142 2 1 -12
b  31  4 3 2

论坛徽章:
0
207 [报告]
发表于 2007-12-13 16:27 |只看该作者

回复 #206 zqc99 的帖子

你的算法结果一样错
最优的是
a 132 1  1  1
b 13  13 12 12

论坛徽章:
0
208 [报告]
发表于 2007-12-13 16:31 |只看该作者
我的程序没31楼那么好
但一样实现了算法


  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int a[20], b[20];
  5.         int n, i, t;

  6.         /* load data */
  7.         do
  8.         {
  9.                 printf("Set the size of array:");
  10.                 scanf("%d", &n);
  11.         }while (n < 1 || n > 20);

  12.         printf("arry a:");
  13.         for(i = 0;i < n; i++)
  14.                 scanf("%d", a + i);
  15.         printf("arry b:");
  16.         for(i = 0;i < n; i++)
  17.                 scanf("%d", b + i);

  18.         /* sort array a,b and let sum(a) < sum(b) */
  19.         sort(a, n);
  20.         sort(b, n);
  21.         if (sum(a, n) > sum(b, n))
  22.         {       /* exchange a,b */
  23.                 for(i = 0;i < n;i++)
  24.                 {
  25.                         t = a[i];
  26.                         a[i] = b[i];
  27.                         b[i] = t;
  28.                 }
  29.         }

  30.         /* now sum(a) <= sum(b),if lower to make sum(a) near to sum(b) */
  31.         if (sum(a, n) < sum(b, n))
  32.                 make_near(0 , n, a ,b);

  33.         /* output the result */
  34.         sort(a, n);
  35.         puts("\nsum a:");
  36.         for (i = 0;i < n;i++)
  37.                 if (i < n - 1)
  38.                         printf("%d+", a[i]);
  39.                 else
  40.                         printf("%d = ", a[i]);
  41.         printf("%d\n", sum(a, n));

  42.         puts("sum b:");
  43.         for (i = 0;i < n;i++)
  44.                 if (i < n - 1)
  45.                         printf("%d+", b[i]);
  46.                 else
  47.                         printf("%d = ", b[i]);
  48.         printf("%d\n", sum(b, n));

  49.         printf("value between a and b is:%d\n", sum(b, n) - sum(a, n));

  50.         return 0;
  51. }

  52. /* sort the array: small to big */
  53. void sort(int *arr, int n)
  54. {
  55.         int i, j;

  56.         for (i = n - 1;i > 0;i--)
  57.                 for(j = 1;j <= i ;j++)
  58.                 {
  59.                         if (arr[j - 1] > arr[j])
  60.                                 exchange_p(arr + (j - 1), arr + j);
  61.                 }
  62. }

  63. int sum(int *arr, int n)
  64. {
  65.         if (n == 1)
  66.                 return *arr;

  67.         return *arr + sum(arr + 1, n - 1);
  68. }

  69. /* exchange tow pointer value */
  70. void exchange_p(int *a, int *b)
  71. {
  72.         int t;

  73.         t = *a - *b;
  74.         *b += t;
  75.         *a -= t;
  76. }

  77. /* exchange the elements of array a and b,
  78.    to make sum(a) nearest to sum(b),
  79.    and make sure sum(b) always greater than sum(a),
  80.    if not,should exchange the elements back.
  81.    the condition of return: sum(a) == sum(b) or m point to the end of a
  82. */
  83. void make_near(int m, int n, int *a, int *b)
  84. {/* m point to the current element of a, n is the array's size */
  85.         int i;

  86.         for (i = n - 1;i >= 0;i--)
  87.         {
  88.                 if (a[m] < b[i])
  89.                 {
  90.                         exchange_p(a + m, b + i);
  91.                         /* success to exchange and go to next step */
  92.                         if (sum(a, n) < sum (b, n))
  93.                                 break;
  94.                         /* sum(a) == sum(b) ,return */
  95.                         else if (sum(a, n) == sum(b ,n))
  96.                                 return;
  97.                         /* failure to exchange,exchange back */
  98.                         else
  99.                                 exchange_p(a + m, b + i);
  100.                 }
  101.                 else/* it's no need to process a[m] >= b[i],go to next step */
  102.                         break;
  103.         }
  104.         sort(b ,n);

  105.         /* m point to the end of a*/
  106.         if (m == n)
  107.                 return;

  108.         /* m move to the next element */
  109.         make_near(m + 1, n, a, b);
  110. }

复制代码

[ 本帖最后由 UCfree 于 2007-12-13 17:00 编辑 ]

论坛徽章:
0
209 [报告]
发表于 2008-01-07 16:58 |只看该作者
很多人简单一想就回答, 排序, 交叉取数.
这样很多情况下并不能满足要求
其实这题其实没这么简单, 牵涉到一个叫"模拟退火"的算法.
具体我也不太清楚,算法搞不明白. 也懒得看啦.
不过算法有机会还是要系统学学的~

论坛徽章:
0
210 [报告]
发表于 2008-01-07 19:50 |只看该作者
楼上的,你真狠,  处子贴就挖出来个尸体,  你不挖 我还真没看过这贴 .  从头看到尾,  觉得挺有意思,  也发现了一个问题, CU高手潜水严重!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP