Exercise 2-18 Find the number of combinations (15 points)
This question requires writing a program to calculate the number of combinations of m elements (m≤n) from n different elements according to the formula .
It is recommended to define and call the function fact (n) to calculate n !, where the type of n is int and the function type is double.
Input format:
Enter two positive integers m and n (m≤n) in one line, separated by spaces.
Output format: output
according to the format "result = combination number calculation result". The title guarantees that the result is within the double type.
Sample input:
2 7
Sample output:
result = 21
Code:
#include"stdio.h"
double fact(int n)
{
if(n >= 2)
{
return n*fact(n-1);
}
return 1;
}
int main()
{
int m, n;
scanf("%d %d", &m, &n);
printf("result = %0.lf", fact(n)/fact(m)*fact(n-m)));
return 0;
}
The above fact function is recursive. If you don't understand it, you can use Baidu.