我正在尝试使用递归来解决问题,如果我使用“if”语句,这将非常冗长。我想看看 CONST = 50 在 n 中有多少次。我想返回 50 在 n 中出现的次数。我知道这是直截了当的,但我想使用递归来实现这一点,这对我来说并不直截了当。条件如下:
0 < n == 50 -> 1 instance
50 < n <= 100 -> 2 instance
100 < n <= 150 -> 3 instance
150 < n <= 200 -> 4 instance
200 < n <= 250 -> 5 instance
...
...
...
下面是我开始的,但我卡住了:
def num_of_times(n)
""" (int) => int
when n is entered count 1 for every 50 found. If any number is over 50, yet does not
equal another 50 (e.g. n = 60; 60 - 50 = 10; 50 -> 1; 10 -> 1) will call for a count, which would be a count of 2 in this case.
>>>num_of_times(60)
2
>>>num_of_times(200)
4
"""
count = 0
if n > 0 and n == 50:
count += 1
elif n > 50:
""" Here is where my thinking is going to sleep"""
...
...
...
提前感谢您提供的任何帮助。