gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:58 25 4
gpt4 key购买 nike

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

最佳答案

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

关于python - 欧拉计划 #11 : Python solution not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9014001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com