📜  C程序停止站数

📅  最后修改于: 2021-05-28 05:14:42             🧑  作者: Mango

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

例子 –

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

Input  : n = 16, s = 5
Output : 792
#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);
}
输出:
126

有关更多详细信息,请参阅“停车站数量问题”的完整文章!

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。