
问题陈述- 一个程序,用于查找火车在 n 个车站中的 r 个车站停靠的方式,以便没有两个停靠站是连续的。
该程序将计算火车停靠的方式数,即排列。在这里,火车将从点X行驶到Y。在这些点之间,有n个站点。列车将在这n个车站中的r个车站停靠,条件是在r车站停靠时,列车不应在连续两个车站停靠.
可以使用直接的 npr 公式找到此排列。
让我们举几个例子, p>
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</p><p>Number of stopping station = %d</p><p>", 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
以上就是C程序:求解停靠站问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号