gpt4 book ai didi

python - Python 中的回文链长度不正确

转载 作者:行者123 更新时间:2023-12-01 02:39:08 24 4
gpt4 key购买 nike

我觉得这应该可以正常工作,但我收到一个错误,计数比应有的少 1。

def palindrome_chain_length(n):
count = 0
while str(n) != str(n)[::-1] :
n = n+n
count += 1
else:
return count

最佳答案

如果您得到的计数比您想要的少 1,请从 count = 1 开始。在我看来应该是:

n += int(str(n)[::-1]) 

而不是:

n = n + n 

(参见评论@alfasin)。

关于python - Python 中的回文链长度不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45911704/

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