Chinaunix首页 | 论坛 | 博客
  • 博客访问: 245733
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-04-12 23:34:33

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, , is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

--------------------

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
  4. const int num [] = {
  5. 75,
  6. 95, 64,
  7. 17, 47, 82,
  8. 18, 35, 87, 10,
  9. 20, 4, 82, 47, 65,
  10. 19, 1, 23, 75, 3, 34,
  11. 88, 2, 77, 73, 7, 63, 67,
  12. 99, 65, 4, 28, 6, 16, 70, 92,
  13. 41, 41, 26, 56, 83, 40, 80, 70, 33,
  14. 41, 48, 72, 33, 47, 32, 37, 16, 94, 29,
  15. 53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14,
  16. 70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57,
  17. 91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48,
  18. 63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31,
  19.  4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23,
  20. };

  21. enum {
  22.     LEFT,
  23.     RIGHT,
  24.     NONE,
  25. };

  26. int getfloor(int n)
  27. {
  28.     int floor=0;

  29.     while(1) {
  30.         if (floor*(floor+1)/2 > n)
  31.             break;
  32.         floor++;
  33.     }

  34.     return floor-1;
  35. }

  36. int getnum(int floor)
  37. {
  38.     return floor*(floor+1)/2;
  39. }

  40. int left(int *a, int i)
  41. {
  42.     int floor;
  43.     int offset;

  44.     floor = getfloor(i);
  45.     offset = i - getnum(floor);
  46.     if (offset == 0)
  47.         return 0;
  48.     else {
  49.         int index = getnum(floor-1) + offset-1;
  50.         return a[index];
  51.     }
  52. }

  53. int right(int *a, int i)
  54. {
  55.     int floor;
  56.     int offset;

  57.     floor = getfloor(i);
  58.     offset = i - getnum(floor);
  59.     if (offset == floor)
  60.         return 0;
  61.     else {
  62.         int index = getnum(floor-1) + offset;
  63.         return a[index];
  64.     }
  65. }

  66. void output(int *a, int len)
  67. {
  68.     int i;

  69.     for (i=0; i < len; i++)
  70.         printf("%d ", a[i]);
  71.     printf("\n");
  72. }

  73. int judge(int *a, int len)
  74. {
  75.     int max=0;
  76.     int floor;
  77.     int i;

  78.     floor = getfloor(len-1);

  79.     for (i=getnum(floor); i<len; i++) {
  80.         if (a[i] > max)
  81.             max = a[i];
  82.     }

  83.     return max;
  84. }

  85. int main(int argc, const char *argv[])
  86. {

  87.     int i;
  88.     int sum[ARRAY_SIZE(num)];
  89.     int dir[ARRAY_SIZE(num)];

  90.     dir[0] = NONE;
  91.     sum[0] = num[0];
  92.     for (i = 1; i <= ARRAY_SIZE(num); i++) {
  93.         if (left(sum, i) > right(sum, i)) {
  94.             dir[i] = RIGHT;
  95.             sum[i] = num[i] + left(sum, i);
  96.         } else {
  97.             dir[i] = LEFT;
  98.             sum[i] = num[i] + right(sum, i);
  99.         }
  100.     }

  101.     /*
  102.      * output(sum, ARRAY_SIZE(sum));
  103.      * output(dir, ARRAY_SIZE(num));
  104.      */

  105.     printf("result: %d\n", judge(sum, ARRAY_SIZE(sum)));

  106.     return 0;
  107. }

典型的动态规划的思想实现... 呵呵!
阅读(1983) | 评论(1) | 转发(0) |
0

上一篇:tikz/pgf 转成图片

下一篇:批量添加用户

给主人留下些什么吧!~~

linyunxian2011-04-13 00:27:01

同样的办法可计算euler67...
算法产生力量!
将一个在有限时间内不可计算的问题变得可计算!