习题19:

作者: 今年说话算话 | 来源:发表于2017-02-22 19:20 被阅读0次

    Let's say that the 'slide down' is a sum of consecutive numbers from the top to the bottom of the pyramid. As you can see, the longest 'slide down' is 3 + 7 + 4 + 9 = 23

    Your task is to write a function longestSlideDown (in ruby: longest_slide_down) that takes a pyramid representation as argument and returns its' longest 'slide down'.

    找出一条从塔尖往下滑的通道,这条通道所有数字相加是最大的。

       /3/
      \7\ 4 
     2 \4\ 6 
    8 5 \9\ 3
    

    def longest_slide_down(pyramid):
    # TODO: write some code...
    # 从0,0开始
    l = []
    length = len(pyramid)
    def steps(x=0, y=0, step=0):
    step += pyramid[x][y]
    if x == length - 1:
    l.append(step)
    else:
    steps(x+1, y+1, step)
    steps(x+1, y, step)
    steps()
    return max(l)

    相关文章

      网友评论

          本文标题:习题19:

          本文链接:https://www.haomeiwen.com/subject/ydimwttx.html