问题陈述– 一个程序,用于查找火车在 n 个车站中的 r 个车站停靠的方式,以便没有两个停靠站是连续的。
问题解释
该程序将计算火车停靠的方式数,即排列。在这里,火车将从点X行驶到Y。在这些点之间,有n个站点。列车将在这n个车站中的r个车站停靠,条件是在r车站停靠时,列车不应在连续两个车站停靠.
可以使用直接的 npr 公式找到此排列。
让我们举几个例子, p>
Input : n = 16 , r = 6Output : 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.
登录后复制
示例
现场演示
#includeint main(){ int n = 16, s = 6; printf("Total number of stations = %dNumber 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 = 16Number of stopping station = 6Possible ways = 462
登录后复制
以上就是C程序:求解停靠站问题的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582257.html