📜  停靠站数量问题的Python程序

📅  最后修改于: 2022-05-13 01:56:56.555000             🧑  作者: Mango

停靠站数量问题的Python程序

在 A 和 B 两地之间有 12 个中间站。求一列火车在这些中间站中的 4 个停靠点有多少种方式,使得没有两个停靠站是连续的?

例子 -

Input  : n = 12, s = 4
Output : 126

Input  : n = 16, s = 5
Output : 792
# Python code to calculate number
# of ways of selecting \'p\' non 
# consecutive stations out of 
# \'n\' stations
  
def stopping_station( p, n):
    num = 1
    dem = 1
    s = p
  
    # selecting \'s\' positions
    # out of \'n-s+1\'
    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:
        # if conditions does not
        # satisfy of combinatorics
        return -1
  
# driver code 
num = stopping_station(4, 12)
if num != -1:
    print(num)
else:
    print("Not Possible")
  
# This code is contributed by "Abhishek Sharma 44"

输出 :

126

更多详情请参考完整文章停站数问题!