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

nCr에 대한 반복 관계를 사용하여 조합을 계산하는 C++ 프로그램

<시간/>

nCr에 대한 Recurrence Relation을 사용하여 Combinations를 계산하는 C++ 프로그램입니다.

알고리즘

Begin
   function CalCombination():
      Arguments: n, r.
      Body of the function:
      Calculate combination by using
      the formula: n! / (r! * (n-r)!.
End

예시

#include<iostream>
using namespace std;
float CalCombination(float n, float r) {
   int i;
      if(r > 0)
         return (n/r)*CalCombination(n-1,r-1);
      else
   return 1;
}
int main() {
   float n, r;
   int res;
   cout<<"Enter the value of n: ";
   cin>>n;
   cout<<"Enter the value of r: ";
   cin>>r;
   res = CalCombination(n,r);
   cout<<"\nThe number of possible combinations are: nCr = "<<res;
}

출력

Enter the value of n: 7
Enter the value of r: 6
The number of possible combinations are: nCr = 2