关闭

11、“小练习”学习

69人阅读 评论(0) 收藏 举报
分类:

本文代码参照鱼C工作室论坛内容,原文网址:http://bbs.fishc.com/thread-72202-1-2.html

题目:

从下面的三角形的顶端开始,向下面一行的相邻数字移动,从顶端到底端的最大总和为 23.
 
也就是 3 + 7 + 4 + 9 = 23.
找出从以下三角形的顶端走到底端的最大总和:


代码:

data = '''
        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
'''
#将字符串分割成列表
data = data.split()
    '''
     #将data转换成如下样式:
     
    [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]
    '''
num = []
for k in range(1, 16):    
    num.append(list(map(int, data[int(k*(k-1)/2) : int(k*(k+1)/2)])))
for k in range(2, 16):
    '''
    将num列表进行处理得到如下样式:    
    [75]
    [170, 139]
    [187, 47, 221]
    [205, 35, 87, 231]
    [225, 4, 82, 47, 296]
    [244, 1, 23, 75, 3, 330]
    [332, 2, 77, 73, 7, 63, 397]
    [431, 65, 4, 28, 6, 16, 70, 489]
    [472, 41, 26, 56, 83, 40, 80, 70, 522]
    [513, 48, 72, 33, 47, 32, 37, 16, 94, 551]
    [566, 71, 44, 65, 25, 43, 91, 52, 97, 51, 565]
    [636, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 622]
    [727, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 670]
    [790, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 701]
    [794, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 724]
    '''
    #s竖的一列进行累加
    num[k-1][0] = num[k-2][0] + num[k-1][0]
    #斜边上的书累加
    num[k-1][-1] = num[k-2][-1] + num[k-1][-1]
    '''
    将num列表进行处理得到如下样式:    
    [75]
    [170, 139]
    [187, 217, 221]
    [205, 252, 308, 231]
    [225, 256, 390, 355, 296]
    [244, 257, 413, 465, 358, 330]
    [332, 259, 490, 538, 472, 421, 397]
    [431, 397, 494, 566, 544, 488, 491, 489]
    [472, 472, 520, 622, 649, 584, 571, 561, 522]
    [513, 520, 592, 655, 696, 681, 621, 587, 655, 551]
    [566, 591, 636, 720, 721, 739, 772, 673, 752, 706, 565]
    [636, 602, 669, 748, 798, 812, 789, 850, 791, 820, 723, 622]
    [727, 707, 721, 786, 815, 826, 903, 893, 908, 870, 847, 752, 670]
    [790, 793, 725, 854, 904, 879, 970, 933, 981, 924, 939, 934, 792, 701]
    [794, 855, 891, 881, 927, 913, 1040, 1068, 1054, 1074, 977, 992, 994, 796, 724]
    '''
    #将中间的进行累加,同位置取大的数
    if len(num[k-1]) >2 :
        for i in range(1,len(num[k-1])-1):
            num[k-1][i] = max(num[k-2][i-1], num[k-2][i]) + num[k-1][i]
#输出最后一行中最大的数
print(max(num[-1]))


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场