แจ้งปัญหา − โปรแกรมค้นหาจำนวนวิธีที่รถไฟจะหยุดใน r สถานีออกจาก n เพื่อไม่ให้มีสถานีหยุดสองสถานีต่อเนื่องกัน
คำอธิบายปัญหา
โปรแกรมนี้จะทำการคำนวณจำนวนทาง เช่น การเรียงสับเปลี่ยนที่รถไฟจะหยุด ที่นี่รถไฟจะเดินทางจากจุด X ถึง Y . ระหว่างจุดเหล่านี้มี n สถานี รถไฟจะหยุดที่r สถานีของ n . เหล่านี้ สถานีที่กำหนดเงื่อนไขว่าในขณะที่หยุดบน r สถานีที่รถไฟไม่ควรจอดสองสถานีติดต่อกัน
การเปลี่ยนแปลงนี้สามารถพบได้โดยใช้ n . โดยตรง pr สูตร
มาดูตัวอย่างกัน
Input : n = 16 , r = 6 Output : 462
คำอธิบาย − จำนวนวิธีที่รถไฟสามารถหยุดที่ 6 ป้ายจาก 16 จุดจอดที่ตรงตามเงื่อนไขนั้นพบได้โดยใช้สูตรการเรียงสับเปลี่ยนที่กำหนดโดย
น pr หรือ 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\nNumber of stopping station = %d\n", 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