Python 3.x 最小步长为1或2,但可被给定数整除

Python 3.x 最小步长为1或2,但可被给定数整除,python-3.x,Python 3.x,我正在开发一个python程序,我想找到到达顶层所需的最小步数,这样步数应该可以被给定的数字整除,比如说m 以下是我的节目选自: 您可以使用以下选项: # A program to count the number of ways to reach n'th stair # Recurssive program to find n'th fibonacci number def fib(n): if n <= 1: return n return

我正在开发一个python程序,我想找到到达顶层所需的最小步数,这样步数应该可以被给定的数字整除,比如说
m

以下是我的节目选自:

您可以使用以下选项:

# A program to count the number of ways to reach n'th stair 

# Recurssive program to find n'th fibonacci number 
def fib(n): 
    if n <= 1: 
        return n 
    return fib(n-1) + fib(n-2) 

# returns no. of ways to reach s'th stair 
def countWays(s, m): 
    # Add in a division by m, which you pass to the function
    # Cast it as an int to return a whole # and not decimal
    return int(fib(s + 1) / m)

# Driver program 

s = 10
# Set this to whatever
m = 3
print("Number of ways = ", countWays(s, m) )
#一个计算到达第n级楼梯的路径数的程序
#求第n个斐波那契数的递归程序
def纤维(n):
如果n
使用%是一种模运算。这将在除法后提供“余数”。因此,如果您的项目没有余数,它可以被选定的数字整除

# A program to count the number of ways to reach n'th stair 

# Recursive function used by countWays 
def countWaysUtil(n,m): 
    res = [0 for x in range(n)] # Creates list res witth all elements 0 
    res[0],res[1] = 1,1

    for i in range(2,n): 
        j = 1
        while j<=m and j<=i: 
            res[i] = res[i] + res[i-j] 
            j = j + 1 
    return res[n-1] 

# Returns number of ways to reach s'th stair 
def countWays(s,m): 
    return countWaysUtil(s+1, m) 

# Driver Program 
s,m = 4,2
print "Number of ways =",countWays(s,m) 

# Contributed by Harshit Agrawal 
#一个计算到达第n级楼梯的路径数的程序
#countWays使用的递归函数
def countWaysUtil(n,m):
res=[0表示范围内的x(n)]#创建所有元素为0的列表res
res[0],res[1]=1,1
对于范围(2,n)内的i:
j=1

虽然jI通过更新我的问题添加了示例输入,但请检查,当我运行程序时,输出与您的程序不匹配。对于
fib
返回
int
,它给出的是44而不是6
int(fib(s+1)/m
int
m
是拼写
fib(s+1)的一种缓慢(对于大数字来说,不准确)的方法//m
。永远不要这样做,始终使用
/
直接整数楼层除法。不说明代码是否正确(没有仔细查看),而是
int(x/y)
用于
int
x
y
基本上总是错误的。这个答案(在当前状态下)没有意义——仍然试图从OP中导出需求。感谢您的提示,@ShadowRangerI通过更新我的问题添加了示例输入,您能检查一下吗,当我运行程序时,输出与您的程序不匹配。对于输入s=10和m=2,输出应该是6,但您的答案仍然是89。请参考示例输入和exp我在问题
中添加了ected输出,但我想用可被给定数字整除的值(例如m)来过滤它们,你能解释一下这意味着什么吗?请给出
计数方式的结果
那么,在修改后的函数中,您期望得到的结果是什么。89如何被26倍整除?@JerryM.,我在问题的末尾添加了一些示例,请检查。如果没有解释,这些示例完全没有意义@learner@JerryM.,现在添加了说明,其中do
步骤为{2,2,2,1,1}
来自???,其中只有3个可以被2整除????
# A program to count the number of ways to reach n'th stair 

# Recurssive program to find n'th fibonacci number 
def fib(n): 
    if n <= 1: 
        return n 
    return fib(n-1) + fib(n-2) 

# returns no. of ways to reach s'th stair 
def countWays(s, m): 
    # Add in a division by m, which you pass to the function
    # Cast it as an int to return a whole # and not decimal
    return int(fib(s + 1) / m)

# Driver program 

s = 10
# Set this to whatever
m = 3
print("Number of ways = ", countWays(s, m) )
s = 10
m = 2
if s % m == 0:
    print(s)

outputs: 10
# A program to count the number of ways to reach n'th stair 

# Recursive function used by countWays 
def countWaysUtil(n,m): 
    res = [0 for x in range(n)] # Creates list res witth all elements 0 
    res[0],res[1] = 1,1

    for i in range(2,n): 
        j = 1
        while j<=m and j<=i: 
            res[i] = res[i] + res[i-j] 
            j = j + 1 
    return res[n-1] 

# Returns number of ways to reach s'th stair 
def countWays(s,m): 
    return countWaysUtil(s+1, m) 

# Driver Program 
s,m = 4,2
print "Number of ways =",countWays(s,m) 

# Contributed by Harshit Agrawal