Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C 정차역 수 문제 프로그램

<시간/>

문제 설명 − 기차가 r에서 멈출 방법의 수를 찾는 프로그램 n개 중 역 두 개의 정차역이 연속되지 않도록 하십시오.

문제 설명

이 프로그램은 기차가 멈출 순열과 같은 방법의 수를 계산합니다. 여기에서 기차는 X 지점에서 출발합니다. Y로 . 이 지점들 사이에는 n 역. 기차는 r에 정차합니다. 이 n개의 방송국 r 에서 정차하는 조건이 주어진 스테이션 역 기차는 두 개의 연속 역에서 정차하지 않아야 합니다.

이 순열은 직접 n 을 사용하여 찾을 수 있습니다. 피r 공식.

몇 가지 예를 들어보겠습니다.

Input : n = 16 , r = 6
Output : 462

설명 − 조건을 만족하는 16개 정류장 중 6개 정류장에서 열차가 멈출 수 있는 방법의 수는 다음과 같은 순열 공식을 사용하여 구합니다.

nr 또는 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