这是一个 C++ 程序,用于使用 nCr 的递归关系计算组合。
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<<"输入 n 的值: "; cin>>n; cout<<"输入 r 的值: "; cin>>r; res = CalCombination(n,r); cout<<"\nThe number of possible combinations are: nCr = "<<res; }输出结果
输入 n 的值: 7 输入 r 的值: 6 The number of possible combinations are: nCr = 2