📜  停车站数

📅  最后修改于: 2021-05-31 20:28:02             🧑  作者: Mango

在A和B两个地方之间有12个中间车站。请问有多少种方法可以使火车停在这些中间车站中的4个中间车站,这样就不会有两个连续的车站?
例子 –

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

Input  : n = 16, s = 5
Output : 792

说明1:
固定/删除四个停靠点作为固定点,如果在停靠点之间至少有一个停靠站,则以其他方式计算可以在它们之间插入其他停靠站的方式。

A   x   x   x   x   x   x   x   x   B

在这8个非停止站点之间,我们有9个位置,我们选择这9个位置作为这8个站点之间的停止位置。
因此,答案应该是^{9}C_4   = 126
说明2:
如果您知道将难以区别的球组合到不同的盒子中,那么您可以简单地使用, ^{(n-p+1)}C_p   。在此问题中,$ n $是要停止的站点数,而$ p $是要停止的站点数。在这里,停止站就像无法分辨的球,非停止站就像可区分的盒子。
所以, ^{(12-8+1)}C_4   = ^{9}C_4   = 126
代码 –

C++
#include 
int stopping_station(int, int);
 
// function to calculate number
// of ways of selecting 'p' non consecutive
// stations out of 'n' stations
int stopping_station(int p, int n)
{
 
    int num = 1, dem = 1, s = p;
 
    // selecting 's' positions out of 'n-s+1'
    while (p != 1) {
 
        dem *= p;
        p--;
    }
 
    int t = n - s + 1;
    while (t != (n - 2 * s + 1)) {
 
        num *= t;
        t--;
    }
 
    if ((n - s + 1) >= s)
        printf("%d", num / dem);
 
    else
 
        // if conditions does not satisfy of combinatorics
        printf("not possible");
}
 
// driver code
int main()
{
 
    // n is total number of stations
    // s is no. of stopping stations
    int n, s;
 
    // arguments of function are
    // number of stopping station
    // and total number of stations
    stopping_station(4, 12);
}


Java
// Java code to calculate number
// of ways of selecting 'p' non
// consecutive stations out of
// 'n' stations
 
import java.io.*;
import java.util.*;
 
class GFG
{
    public static int stopping_station(int p, int n)
    {
        int num = 1, dem = 1, s = p;
 
        // selecting 's' positions out of 'n-s+1'
        while (p != 1)
        {
            dem *= p;
            p--;
        }
 
        int t = n - s + 1;
        while (t != (n - 2 * s + 1))
        {
            num *= t;
            t--;
        }
 
        if ((n - s + 1) >= s)
            System.out.print(num / dem);
 
        else
            // if conditions does not satisfy of combinatorics
            System.out.print("not possible");
 
        return 0;
    }
 
    public static void main (String[] args)
    {
        // n is total number of stations
        // s is no. of stopping stations
        int n, s;
 
        // arguments of function are
        // number of stopping station
        // and total number of stations
        stopping_station(4, 12);
    }
}
// ""This code is contributed by Mohit Gupta_OMG ""


Python3
# 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"


C#
// C# code to calculate number
// of ways of selecting 'p' non
// consecutive stations out of
// 'n' stations
using System;
 
class GFG {
     
    public static int stopping_station(int p,
                                       int n)
    {
        int num = 1, dem = 1, s = p;
 
        // selecting 's' positions
        // out of 'n-s+1'
        while (p != 1)
        {
            dem *= p;
            p--;
        }
 
        int t = n - s + 1;
        while (t != (n - 2 * s + 1))
        {
            num *= t;
            t--;
        }
 
        if ((n - s + 1) >= s)
            Console.WriteLine(num / dem);
 
        // if conditions does not
        // satisfy of combinatorics
        else
          Console.WriteLine("Not possible");
        return 0;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
         
        // arguments of function are
        // number of stopping station
        // and total number of stations
        stopping_station(4, 12);
    }
}
 
// This code is contributed by vt_m.


PHP
= $s)
        echo $num / $dem;
 
    else
 
        // if conditions does not
        // satisfy of combinatorics
        echo "not possible";
}
 
    // Driver Code
    // n is total number of stations
    // s is no. of stopping stations
    $n; $s;
 
    // arguments of function are
    // number of stopping station
    // and total number of stations
    stopping_station(4, 12);
 
// This code is contributed by anuj_67.
?>


Javascript


输出 :

126