卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

C程序:求解停靠站问题

C程序:求解停靠站问题

问题陈述- 一个程序,用于查找火车在 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>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");
}</p>

输出

'
Total number of stations = 16
Number of stopping station = 6
Possible ways = 462
卓越飞翔博客
上一篇: PHP7底层开发原理解读:探索PHP字节码和中间代码的生成过程
下一篇: PHP8 的新特性如何通过实际编写代码进行演示和实践
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏