Skip to main content
 首页 » 编程设计

python之欧拉计划 #11 : Python solution not working

2025年05月04日13qlqwjy

What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid?

http://projecteuler.net/problem=11

我从数字创建了二维元组 LIST。

LIST = ((8, 2, 22, ....), (49, 49, 99, ....) ...)

这是我的解决方案。我无法找出逻辑缺陷。

(编辑):我的答案是 51267216,但不正确。

# For every number check diagnolly, down and right if the product exceeds max_product.  
# Ignore the exception(OutOfBounds) if encountered. 
max_product = 0 
 
for i in range(20): 
    for j in range(20): 
            temp = 1 
            try:  
                for k in range(4): 
                    temp = temp * LIST[i][j+k] 
                if (temp > max_product): 
                    max_product = temp 
            except: 
                pass 
 
            temp = 1 
            try:  
                for k in range(4): 
                    temp = temp * LIST[i+k][j] 
                if (temp > max_product): 
                    max_product = temp 
            except: 
                pass 
 
            temp = 1 
            try: 
                for k in range(4): 
                    temp = temp * LIST[i+k][j+k] 
                if (temp > max_product): 
                    max_product = temp 
            except: 
                pass 
 
 
print max_product 

请您参考如下方法:

您是否忘记了有两个对角线方向?