我正在学习如何用python编写代码,所以我接受了编写代码的挑战,所以我在第14天的第2部分中遇到了一个错误,它是说内存错误,我必须找到846601的答案,看看问题在问什么https://adventofcode.com/2018/day/14,对于隐藏的第二部分,请看这里:http://prntscr.com/lv703p。你知道吗
所以我用的代码
recipe = ["3","7"]
position1 = 1
position2 = 0
value = 0
while value== 0:
newrecipe = str(int(recipe[position2])+int(recipe[position1]))
if len(newrecipe)== 1:
recipe.append(newrecipe)
elif len(newrecipe)== 2:
recipe.append(newrecipe[0])
recipe.append(newrecipe[1])
position2= (position2 + int(recipe[position2])+1) % (len(recipe))
position1= (position1 + int(recipe[position1])+1) % (len(recipe))
if int(recipe[len(recipe)-6]) == 8:
if int(recipe[len(recipe)-5]) == 4:
if int(recipe[len(recipe)-4]) == 6:
if int(recipe[len(recipe)-3]) == 6:
if int(recipe[len(recipe)-2]) == 0:
if int(recipe[len(recipe)-1]) == 1:
value = len(recipe)-5
print(value)
如果你有任何意见或问题,如果我的代码是混乱的,你可以问。你知道吗
编辑: 因此,我使用注释将代码更改为:
recipe = ["3","7"]
position1 = 1
position2 = 0
value=0
while value == 0:
newrecipe = str(int(recipe[position2])+int(recipe[position1]))
if len(newrecipe)== 1:
recipe.append(newrecipe)
elif len(newrecipe)== 2:
recipe.append(newrecipe[0])
recipe.append(newrecipe[1])
position2= (position2 + int(recipe[position2])+1) % (len(recipe))
position1= (position1 + int(recipe[position1])+1) % (len(recipe))
if len(recipe) >= 6 and recipe[-6:] == ["8", "4", "6", "6", "0", "1"]:
value= len(recipe)-5
print(value)
以
如果
len(recipe)
低于6,则可能会导致错误,可能是这样。你可以用类似我意识到错误在于,一旦我将配方列表中的字符串改为整数,我可以使用的字符串的最大限制就没有值那么大了。谢谢你的帮助。你知道吗
相关问题 更多 >
编程相关推荐