gpt4 book ai didi

python - 这是我的递归方法的问题吗?

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:16 25 4
gpt4 key购买 nike

回应this question我试图想出自己的解决方案。

我在 python 3.X 中的解决方案

def efficientalgo(number, x):
print("Start with this number... " + str(number))

# See if a number is dividable by three, if so then divide by 3
if number % 3 == 0:
print("Dividing three...")
number /= 3
print(number)
# use recursion to see if the number can be divided again
number += efficientalgo(number, x)
print("Returning number from division by three now...")
return number


# Divide a number by 2 if it is evenly dividable by 2
if number % 2 == 0:
print("Dividing two...")
number /= 2
print(number)
# Use recursion to see if the number can be divided again
number += efficientalgo(number, x)
print("Returning number from division by two now...")
print(number)
return number


# If a number is not one, then subtract one and call the
if number != 1:
print("Subtracting one now...")
number -= 1
print(number)
# Use recursion to see if the number can be divided again
number += efficientalgo(number, x)
print(number)
return number

# If the number is one, return it and finish.
if number == 1:
print("Returning one now... " + str(number))
return number

print(efficientalgo(100, 1))

这是一个“工作”pythonfiddle

输出

Start with this number... 100
Dividing two...
50.0
Start with this number... 50.0
Dividing two...
25.0
Start with this number... 25.0
Subtracting one now...
24.0
Start with this number... 24.0
Dividing three...
8.0
Start with this number... 8.0
Dividing two...
4.0
Start with this number... 4.0
Dividing two...
2.0
Start with this number... 2.0
Dividing two...
1.0
Start with this number... 1.0
Returning one now... 1.0
-----Above this line is the correct output I want-----

---------Below this line, I have no idea what is going on--------
Returning number from division by two now...
2.0
Returning number from division by two now...
4.0
Returning number from division by two now...
8.0
Returning number from division by three now...
40.0
Returning number from division by two now...
65.0
Returning number from division by two now...
115.0
115.0

正如你所看到的,我错误地实现了我的递归,并得到了某种反馈循环,程序中得到了正确的答案,然后在我返回我的最终数字(在本例中是一个)后继续前进。

我不明白我在输出中标记的行下面发生了什么

最佳答案

数字应设置为= effectivealgo(..)而不是+=:

def efficientalgo(number):
print("Start with this number... " + str(number))
# See if a number is dividable by three, if so then divide by 3
if number % 3 == 0:
print("Dividing three...")
number /= 3
print(number)
# use recursion to see if the number can be divided again
number = efficientalgo(number)
# Divide a number by 2 if it is evenly dividable by 2
if number % 2 == 0:
print("Dividing two...")
number /= 2
print(number)
# Use recursion to see if the number can be divided again
number = efficientalgo(number)

# If a number is not one, then subtract one and call the
if number != 1:
print("Subtracting one now...")
number -= 1
print(number)
# Use recursion to see if the number can be divided again
number = efficientalgo(number)
return number

一旦你这样做了,你就会得到预期的输出:

In [4]: efficientalgo(100)
Start with this number... 100
Dividing two...
50.0
Start with this number... 50.0
Dividing two...
25.0
Start with this number... 25.0
Subtracting one now...
24.0
Start with this number... 24.0
Dividing three...
8.0
Start with this number... 8.0
Dividing two...
4.0
Start with this number... 4.0
Dividing two...
2.0
Start with this number... 2.0
Dividing two...
1.0
Start with this number... 1.0
Out[4]: 1.0

或者直接返回:

def efficientalgo(number):
print("Start with this number... " + str(number))
# See if a number is dividable by three, if so then divide by 3
if number % 3 == 0:
print("Dividing three...")
number /= 3
print(number)
# use recursion to see if the number can be divided again
return efficientalgo(number)

# Divide a number by 2 if it is evenly dividable by 2
if number % 2 == 0:
print("Dividing two...")
number /= 2
print(number)
# Use recursion to see if the number can be divided again
return efficientalgo(number)

# If a number is not one, then subtract one and call the
if number != 1:
print("Subtracting one now...")
number -= 1
print(number)
# Use recursion to see if the number can be divided again
return efficientalgo(number)

return number

关于python - 这是我的递归方法的问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758507/

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