24小时热门版块排行榜    

CyRhmU.jpeg
查看: 1275  |  回复: 12
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

holmescn

金虫 (正式写手)

[交流] Euler 工程 第十八题:三角阵上最大的和已有4人参与

已知从下面这个三角阵的顶端到底端,最大的和是 3 + 7 + 4 + 9 = 23

      3
    7  4
  2  4  6
8  5  9  3

那么下面这个三角阵,从顶到底的最大的和是多少?

                            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

哈哈,睡觉先
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

huycwork

金虫 (著名写手)

★ ★ ★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
微尘、梦想(金币+5): 谢谢参与! 2011-05-31 20:21:56
这题要动态规划下,空间换时间,C++实现如下:
CODE:
#include

enum { Row = 15, Col = 15 };

int buf[] = {
        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, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23,
};

int buf2[sizeof(buf)];

int &at(int *buf, int i, int j){
        --i;
        return buf[j+(1+i)*i/2];
}

size_t eular18(){
        at(buf2, 1, 0) = at(buf, 1, 0);
        for(int i = 2; i <= Row; ++i){
                at(buf2, i, 0) = at(buf2, i-1, 0) + at(buf, i, 0);
                for(int j = 1; j < i-1; ++j){
                        at(buf2, i, j) = std::max(at(buf, i, j)+at(buf2, i-1, j-1), at(buf, i, j)+at(buf2, i-1, j));
                }
                at(buf2, i, i-1) = at(buf2, i-1, i-2) + at(buf, i, i-1);
        }
        int max = 0;
        for(int i = 0; i < Col; ++i){
                max = std::max(max, at(buf2, Row, i));
        }
        return max;
}
int main(){
        std::cout<         return 0;
}

漩涡的中心有一块空地,空空的。
3楼2011-05-31 12:01:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 13 个回答

libralibra

至尊木虫 (著名写手)

骠骑将军

★ ★ ★ ★ ★
微尘、梦想(金币+5): 谢谢参与! 2011-05-31 20:21:27
倒推回去就行了,这个题挺简单
CODE:
%% Find the maximum total from top to bottom of the triangle below:
function result = euler18()
tic;
s = {[ 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 ]};
result = maxRoute(s);
toc;
end

%% compute max route in triangle
% called by 18 and 67
function result = maxRoute(s)
m = length(s);
for i=m-1:-1:1 % 从倒数第二层倒退
    for j=1:length(s{i}) % 每个元素加上下一层邻接2个数中最大的
        s{i}(j) = s{i}(j)+max(s{i+1}(j),s{i+1}(j+1));
    end
end
result = s{1}(1);
end

时间+结果
CODE:
% Elapsed time is 0.002799 seconds.
% ans =
%         1074

[ Last edited by libralibra on 2011-6-2 at 15:26 ]
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2011-05-31 02:05:24
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wangww2011

木虫 (著名写手)


小木虫(金币+0.5):给个红包,谢谢回帖
微尘、梦想: 这样没有题目,后面来的人是看不懂的啊,还是按题做吧! 2011-05-31 20:24:04
那啥 这题确实简单 直接做67题吧
67题和18题一样,只不过数据多一些而已,解法是一样的
数据见附件
CODE:
7273
elapsed time=0.000000 seconds.

代码
CODE:
#include
#include
#include

#define TIMERSTART clock_t start_time,stop_time;double elapsed_time;start_time = clock();
#define TIMERSTOP stop_time = clock();elapsed_time=(double)(stop_time-start_time)/CLOCKS_PER_SEC;printf("elapsed time=%f seconds.\n",elapsed_time);


static const int n=100;

int euler67(){
  int i,j,sum=0;
  int data[n][n];
  int path[n];
  
  FILE *fp=fopen("triangle.txt", "r");
  if(fp == 0) return -1;

    for(i = 0; i < n; i++) {
       for(j = 0; j          fscanf(fp, "%d", &data[i][j]);
        }
    }
    fclose(fp);

   for(i = n-1; i > 0; i--) {
      for(j = 0; j         if(data[i][j]>data[i][j+1]) data[i-1][j]+=data[i][j];
        else data[i-1][j]+=data[i][j+1];
      }
   }
   
  return data[0][0];
}


int main(void){
int i;

TIMERSTART;

printf("%d\n",euler67());
  
TIMERSTOP;

  return 0;
}

[ Last edited by wangww2011 on 2011-5-31 at 20:38 ]
4楼2011-05-31 19:06:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

holmescn

金虫 (正式写手)

★ ★ ★ ★ ★
微尘、梦想(金币+5): 谢谢参与! 2011-06-02 14:29:39
这个题,写个穷举还挺麻烦。虽然速度也不慢。看我的quick & dirty的Python版
CODE:
# -*- coding: utf-8 -*-
m = [[75],
    [95,64],
    [17,47,82],
    [18,35,87,10],
    [20,4,82,47,65],
    [19,1,23,75,3,34],
    [88,2,77,73,7,63,67],
    [99,65,4,28,6,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,4,68,89,53,67,30,73,16,69,87,40,31],
    [4,62,98,27,23,9,70,98,73,93,38,53,60,4,23]]

# 每一行的当前位置
pos = [0] * 15

# 最大值和最大值所在的位置
maxPos = pos
maxSum = 0

while pos[0] == 0:
    s = 0
    for i in range(15):
        s += m[i][pos[i]]

    if maxSum < s:
        maxSum = s
        maxPos = [x for x in pos]

# 查找移动后仍符合相邻的那个点
    i = 14
    while pos[i] + 1 > pos[i-1] + 1: i -= 1
# 移动这个点
    pos[i] += 1
# 移动这个点下面的点
    for j in range(i, 14):
        pos[j+1] = pos[j]

# 输出结果
print maxSum
print maxPos
print [m[i][maxPos[i]] for i in range(15)]

本来想用递归的,怎么也写不好,后来放弃了。没有试用于第67题,所以不知道那个会怎么样。反正这个题是可以秒杀的。

上一个数格子的题和这个有点类似。不过大家都喜欢用数学方法解了,没人用数的方法。

又说这个东西,有点像一个二叉树的遍历。但又好像不是。想不明白了。
5楼2011-06-02 12:16:51
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复(可上传附件)
信息提示
请填处理意见