停止站问题数目的 C 程序
问题陈述− 一个程序,它可以找出列车在 n 个站中停在 r 个站的方法,前提是不允许连续停靠在两个站上。
问题说明
该程序将计算火车停车方法的数量,即排列次数。火车将在 X 点到 Y 点之间行驶。在这两个点之间有 n 个站。火车将在这些 n 个站中的 r 个站停靠,条件是,在 r 个站停靠期间,火车不得连续停靠在两个站上。
可以使用直接的 npr 公式找出此排列。
我们举几个例子,
Input : n = 16 , r = 6 Output : 462
说明− 满足条件,火车在 16 个站可停靠的 6 个站中的停靠方法,可以使用排列公式计算得出,如下所示:
npr 或 p(n, r) = n! ∕ (n-r)!
算法
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
Number of stopping station = %d
", 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP