zoukankan      html  css  js  c++  java
  • Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11

    # Project Euler: Problem 10: Largest product in a grid
    # In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
    # The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
    # What is the greatest product of four adjacent numbers in the same direction
    # (up, down, left, right, or diagonally) in the 20×20 grid?
    # Answer: 70600674
    
    n1 = '08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08'.split(' ')
    n2 = '49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00'.split(' ')
    n3 = '81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65'.split(' ')
    n4 = '52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91'.split(' ')
    n5 = '22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80'.split(' ')
    n6 = '24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50'.split(' ')
    n7 = '32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70'.split(' ')
    n8 = '67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21'.split(' ')
    n9 = '24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72'.split(' ')
    n10 = '21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95'.split(' ')
    n11 = '78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92'.split(' ')
    n12 = '16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57'.split(' ')
    n13 = '86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58'.split(' ')
    n14 = '19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40'.split(' ')
    n15 = '04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66'.split(' ')
    n16 = '88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69'.split(' ')
    n17 = '04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36'.split(' ')
    n18 = '20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16'.split(' ')
    n19 = '20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54'.split(' ')
    n20 = '01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'.split(' ')
    
    lst = []
    lst.append(n1)
    lst.append(n2)
    lst.append(n3)
    lst.append(n4)
    lst.append(n5)
    lst.append(n6)
    lst.append(n7)
    lst.append(n8)
    lst.append(n9)
    lst.append(n10)
    lst.append(n11)
    lst.append(n12)
    lst.append(n13)
    lst.append(n14)
    lst.append(n15)
    lst.append(n16)
    lst.append(n17)
    lst.append(n18)
    lst.append(n19)
    lst.append(n20)
    
    prod = []
    for i in range(17):  #向右下方向的4个数字
        for j in range(17):
            prod.append(int(lst[i][j]) * int(lst[i+1][j+1]) * int(lst[i+2][j+2]) * int(lst[i+3][j+3]))
    for i in range(3,20):  #向右上方向的4个数字
        for j in range(17):
            prod.append(int(lst[i][j]) * int(lst[i-1][j+1]) * int(lst[i-2][j+2]) * int(lst[i-3][j+3]))
    for i in range(17):  #纵向的4个数字
        for j in range(20):
            prod.append(int(lst[i][j]) * int(lst[i+1][j]) * int(lst[i+2][j]) * int(lst[i+3][j]))
    for i in range(20):  #横向的4个数字
        for j in range(17):
            prod.append(int(lst[i][j]) * int(lst[i][j+1]) * int(lst[i][j+2]) * int(lst[i][j+3]))
    print(max(prod))
    

    就是有个 20 * 20 的数字网格,求任意直线方向(横、竖、斜)4个数字相乘最大的乘积。

    做完这一题,深深感觉到写代码真是体力劳动…… 话说,把 n1-n20 放进 lst 列表,就没有别的简便的方法吗?我可是一个一个给 append() 进去的啊……

    好容易组成一个二维的 prod 列表,接着 4 个方向 4 个数字相乘也挺费打字的。真心累……

    ++++++++++++++++++++++++++++++++++

    【2016-10-31 更新 】codegay 提醒,发现 Project Euler 相应论坛里有个叫 wcb 的人贴出了好方法,可以不用像我这么辛苦地打字。不过这个人还新增一个 for 循环,把4个方向的数字相乘的情况也循环进去了,算法太美我无法理解,只能算了。逻辑思维能力差,把功能学到手就好了,就不烧脑学高级算法了。所以改进的地方主要是针对字符串多行拆分,代码如下:

    numbers = """08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
    49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
    81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
    52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
    22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
    24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
    32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
    67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
    24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
    21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
    78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
    16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
    86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
    19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
    04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
    88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
    04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
    20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
    20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
    01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"""
    
    lst = [list(int(number) for number in row.split(' ')) for row in numbers.split('
    ')]
    
    prod = []
    for i in range(17):  #向右下方向的4个数字
        for j in range(17):
            prod.append(int(lst[i][j]) * int(lst[i+1][j+1]) * int(lst[i+2][j+2]) * int(lst[i+3][j+3]))
    for i in range(3,20):  #向右上方向的4个数字
        for j in range(17):
            prod.append(int(lst[i][j]) * int(lst[i-1][j+1]) * int(lst[i-2][j+2]) * int(lst[i-3][j+3]))
    for i in range(17):  #纵向的4个数字
        for j in range(20):
            prod.append(int(lst[i][j]) * int(lst[i+1][j]) * int(lst[i+2][j]) * int(lst[i+3][j]))
    for i in range(20):  #横向的4个数字
        for j in range(17):
            prod.append(int(lst[i][j]) * int(lst[i][j+1]) * int(lst[i][j+2]) * int(lst[i][j+3]))
    print(max(prod))
    

    即是说,Python 可以用三个单引号或双引号,把20行的字符都赋值给变量 numbers,然后用字符串拆分的方法,先用 numbers.split(' ') 把 20 行分解出来,再用 row.split(' ') 把各个数字拆分出来。此时拆分出来的数字其实是字符串,所以得用 int() 转为数字。而 int(number) for number in row.split(' ') 生成的好像只是个“生成器”(?),所以还得用 list() 转换为列表。呼~~~~~~ 有够复杂的,但简洁好用啊!

    lst 这个二维列表建立起来之后,就剩下四个方向的判断了。虽然我的代码复杂一些,但浅显好理解啊,也算是一种优点吧~~~ :)

  • 相关阅读:
    windows系统切换jdk,修改java_home无效情况
    Cannot instantiate interface org.springframework.context.ApplicationListener
    MySQL分组查询获取每个学生前n条分数记录(分组查询前n条记录)
    ASP.NET Web API 使用Swagger生成在线帮助测试文档,支持多个GET
    EF TO MYSQL 无法查询中文的解决方法
    HttpWebRequest post请求获取webservice void数据信息
    This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案
    MySQL 5.7.13解压版安装记录 mysql无法启动教程
    C# udpclient 发送数据断网后自动连接的方法
    汽车XX网站秒杀抢购代码
  • 原文地址:https://www.cnblogs.com/iderek/p/6011938.html
Copyright © 2011-2022 走看看