C程序停止站数
问题陈述-一个程序,该程序从n个站点中找出r个站点中火车停站的方式,以使两个停靠站都不连续。
问题说明
该程序将计算列车停止的方式(即排列)的数量。在这里,火车将从X点行驶到Y点。在这些点之间,有n个站。列车将停止对[R这些台站ñ给出的条件,而在停止站[R站列车不应该在连续两个站停止。
可以使用直接npr公式找到此排列。
让我们举几个例子
Input : n = 16 , r = 6 Output : 462
说明-满足条件的16个站点中,火车可以在6个站点中停止的方式数量可以通过以下公式得出:
npr或p(n,r)=n!∕(nr)!
算法
Input : total numbers of stations n and number of stations train can stop r. Step 1 : For values of n and r calculate the value of p(n,r) = n! / (n-r)! Step 2 : print the value of p(n,r) using std print method.
示例
#include<stdio.h>
int main(){
int n = 16, s = 6;
printf("Total number of stations = %d\nNumber of stopping station = %d\n", s, n);
int p = s;
int num = 1, dem = 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("Possible ways = %d", num / dem);
else
printf("no possible ways");
}输出结果
Total number of stations = 16 Number of stopping station = 6 Possible ways = 462