停止站問題數目的 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