Chinaunix首页 | 论坛 | 博客
  • 博客访问: 151401
  • 博文数量: 40
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 908
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-03 11:03
个人简介

学习linux

文章分类
文章存档

2014年(7)

2013年(33)

我的朋友

分类: C/C++

2014-03-29 20:08:24

刚刚参加了实习生笔试,最后是很有趣的一个算法题,回来没事就把代码写出来了。

题目大概:

一个数组:A[10]每个值代表矩形的高度,求最大的矩形大小。
比如A[10]= { 1, 2 , 4, 6, 0 ,5, 3 ,0 ,2 ,2};
1表示横坐标为1的单元高度为1; 6就表示横坐标为4的单元高度为6;
很明显最大是4和6组成的矩形,面积为8

下面是我写的程序:(不好意思,这个有bug,看最后一个改好的程序)

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. int find_max(int A[],int left,int right);

  3. //only one zero
  4. int main(int argc,char *argv[])
  5. {
  6.     int ret;

  7.     int A[10] = {6,7,9,1,13,12,2,4,20,4};

  8.     ret = find_max(A,0,9);

  9.     printf("%d\n",ret);

  10.     return 0;
  11. }

  12. void change(int A[],int left,int right,int d0)
  13. {
  14.     int i;
  15.     for(i=left;i<=right;i++)
  16.     {
  17.         A[i] = A[i] - d0;
  18.         printf("%d ",A[i]);
  19.     }
  20.     printf("\n\n");
  21. }

  22. int which_max(int a,int b,int c)
  23. {
  24.     int tmp;
  25.     if(a>b)
  26.         tmp = a;
  27.     else
  28.         tmp = b;

  29.     if(c>tmp)
  30.         return c;
  31.     else
  32.         return tmp;
  33. }

  34. void min_height(int A[],int left,int right,int *i0,int *d0)
  35. {
  36.     int i;
  37.     
  38.     *i0 = left;
  39.     *d0= A[left];

  40.     for(i=left+1;i<=right;i++)
  41.     {
  42.         if(A[i]<*d0)
  43.         {
  44.             *i0 = i;
  45.             *d0 = A[i];
  46.         }
  47.     }
  48. }
  49.     

  50. int find_max(int A[],int left,int right)
  51. {
  52.     int i0,d0;
  53.     int base_max;
  54.     int left_max;
  55.     int right_max;
  56.     int max;

  57.     min_height(A,left,right,&i0,&d0);

  58.     base_max = (right - left + 1)*d0;

  59.     printf("find base_max = %d int %d to %d,middle is %d ,height %d\n",
  60.         base_max,left,right,i0,d0);

  61.     change(A,left,right,d0);


  62.     if(left == i0)
  63.         left_max = 0;
  64.     else
  65.     {
  66.         left_max = find_max(A,left,i0-1);
  67.         left_max = left_max + (i0-left)*d0;
  68.     }
  69.     
  70.     if(right == i0)
  71.         right_max = 0;
  72.     else
  73.     {
  74.         right_max = find_max(A,i0+1,right);
  75.         right_max = left_max + (right-i0)*d0;
  76.     }
  77.     
  78.     
  79.     max = which_max(base_max,left_max,right_max);
  80.     printf("base_max %d ,left_max %d ,right_max %d max %d\n\n",
  81.         base_max,left_max,right_max,max);

  82.     return max;
  83. }
下面是运行结果:(错误程序运行结果,看下面正确程序的运行结果)

点击(此处)折叠或打开

  1. root@archer:/work/user-lib/test# ./tencent
  2. find base_max = 10 int 0 to 9,middle is 3 ,height 1
  3. 5 6 8 0 12 11 1 3 19 3

  4. find base_max = 15 int 0 to 2,middle is 0 ,height 5
  5. 0 1 3

  6. find base_max = 2 int 1 to 2,middle is 1 ,height 1
  7. 0 2

  8. find base_max = 2 int 2 to 2,middle is 2 ,height 2
  9. 0

  10. base_max 2 ,left_max 0 ,right_max 0 max 2

  11. base_max 2 ,left_max 0 ,right_max 1 max 2

  12. base_max 15 ,left_max 0 ,right_max 10 max 15

  13. find base_max = 6 int 4 to 9,middle is 6 ,height 1
  14. 11 10 0 2 18 2

  15. find base_max = 20 int 4 to 5,middle is 5 ,height 10
  16. 1 0

  17. find base_max = 1 int 4 to 4,middle is 4 ,height 1
  18. 0

  19. base_max 1 ,left_max 0 ,right_max 0 max 1

  20. base_max 20 ,left_max 11 ,right_max 0 max 20

  21. find base_max = 6 int 7 to 9,middle is 7 ,height 2
  22. 0 16 0

  23. find base_max = 0 int 8 to 9,middle is 9 ,height 0
  24. 16 0

  25. find base_max = 16 int 8 to 8,middle is 8 ,height 16
  26. 0

  27. base_max 16 ,left_max 0 ,right_max 0 max 16

  28. base_max 0 ,left_max 16 ,right_max 0 max 16

  29. base_max 6 ,left_max 0 ,right_max 4 max 6

  30. base_max 6 ,left_max 22 ,right_max 25 max 25

  31. base_max 10 ,left_max 18 ,right_max 24 max 24

  32. 24
说明最大面积是 24,很显然就是 12和13组成的矩形。


正确的程序:

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. int find_max(int A[],int left,int right);


  3. int main(int argc,char *argv[])
  4. {
  5.     int ret;

  6.     int A[10] = {6,7,0,0,13,12,2,2,20,3};
  7. //    int A[] = {2,18,1};

  8.     ret = find_max(A,0,9);

  9.     printf("%d\n",ret);

  10.     return 0;
  11. }

  12. void change(int A[],int left,int right,int d0)
  13. {
  14.     int i;
  15.     for(i=left;i<=right;i++)
  16.     {    
  17.         A[i] = A[i] - d0;
  18.         printf("%d ",A[i]);
  19.     }
  20.     printf("\n");
  21. }

  22. int which_max(int a,int b,int c)
  23. {
  24.     int tmp;
  25.     if(a>b)
  26.         tmp = a;
  27.     else
  28.         tmp = b;

  29.     if(c>tmp)
  30.         return c;
  31.     else
  32.         return tmp;
  33. }

  34. void min_height(int A[],int left,int right,int *i0,int *d0)
  35. {
  36.     int i;
  37.     
  38.     *i0 = left;
  39.     *d0= A[left];

  40.     for(i=left+1;i<=right;i++)
  41.     {
  42.         if(A[i]<*d0)
  43.         {
  44.             *i0 = i;
  45.             *d0 = A[i];
  46.         }
  47.     }
  48. }
  49.     

  50. int find_max(int A[],int left,int right)
  51. {
  52.     int i0,d0;
  53.     int base_max;
  54.     int left_max;
  55.     int right_max;
  56.     int max;

  57.     if(right == left)
  58.     {
  59.         printf("%d \n",A[left]);
  60.         return A[left];
  61.     }

  62.     min_height(A,left,right,&i0,&d0);

  63.     base_max = (right - left + 1)*d0;

  64.     change(A,left,right,0);
  65.     
  66.     printf("find base_max = %d int %d to %d,middle is %d ,height %d\n",
  67.         base_max,left,right,i0,d0);

  68. //    change(A,left,right,d0);


  69.     if(left == i0)
  70.         left_max = d0;
  71.     else
  72.     {
  73.         left_max = find_max(A,left,i0-1);
  74. //        left_max = left_max + (i0-left)*d0;
  75.     }
  76.     
  77.     if(right == i0)
  78.         right_max = d0;
  79.     else
  80.     {
  81.         right_max = find_max(A,i0+1,right);
  82. //        right_max = right_max + (right-i0)*d0;
  83.     }
  84.     
  85.     
  86.     max = which_max(base_max,left_max,right_max);
  87.     printf("base_max %d ,left_max %d ,right_max %d max %d\n\n",
  88.         base_max,left_max,right_max,max);

  89.     return max;
  90. }

正确的运行结果如下:

root@archer:/work/user-lib/test# ./tencent 
6 7 0 0 13 12 2 2 20 3 
find base_max = 0 int 0 to 9,middle is 2 ,height 0
6 7 
find base_max = 12 int 0 to 1,middle is 0 ,height 6

base_max 12 ,left_max 6 ,right_max 7 max 12


0 13 12 2 2 20 3 
find base_max = 0 int 3 to 9,middle is 3 ,height 0
13 12 2 2 20 3 
find base_max = 12 int 4 to 9,middle is 6 ,height 2
13 12 
find base_max = 24 int 4 to 5,middle is 5 ,height 12
13 
base_max 24 ,left_max 13 ,right_max 12 max 24


2 20 3 
find base_max = 6 int 7 to 9,middle is 7 ,height 2
20 3 
find base_max = 6 int 8 to 9,middle is 9 ,height 3
20 
base_max 6 ,left_max 20 ,right_max 3 max 20


base_max 6 ,left_max 2 ,right_max 20 max 20


base_max 12 ,left_max 24 ,right_max 20 max 24


base_max 0 ,left_max 0 ,right_max 24 max 24


base_max 0 ,left_max 12 ,right_max 24 max 24


24


阅读(2762) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~