停靠站数量问题的 Python 程序

pythonserver side programmingprogramming

在本文中,我们将了解下面给出的问题陈述的解决方案。

问题陈述− 已知两个地点 A 和 B 之间有 13 个中间站。我们需要找出有多少种方法可以让火车在 2 个中间站停靠,从而避免出现连续的车站?

现在让我们观察下面实现中的解决方案 −

示例

# 停靠站
def stopping_station( p, n):
   num = 1
   dem = 1
   s = p
   # 选择指定位置
   while p != 1:
      dem *= p
      p-=1
   t = n - s + 1
   while t != (n-2 * s + 1):
      num *= t
      t-=1
   if (n - s + 1) >= s:
      return int(num/dem)
   else:
      # condition
      return -1
# main
num = stopping_station(2, 13)
if num != -1:
   print("No of stopping stations:",num)
else:
   print("I'm Possible")

输出

No of stopping stations: 66

所有变量均在局部范围内声明,其引用如上图所示。

结论

在本文中,我们了解了如何为停车站数量问题编写 Python 程序。


相关文章