Exercise 2-18 Find the number of combinations (15 points)
This question requires writing a program, according to the formula C mn = n! M! (N − m)! C^n_m=\frac{n!}{m!(n−m)!}Cmn=m!(n−m)!n!Calculate the number of combinations of m elements (m≤n) from n different elements.
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:
Input two positive integers m and n (m≤n) in one line, separated by spaces.
Output format: output
according to the format "result = calculation result of the number of combinations". The title guarantees that the result is within the double type range.
Input sample:
2 7
Output sample:
result = 21
Author
Yan Hui
Unit
Zhejiang University City College
Code length limit
16 KB
Time limit
400 ms
Memory limit
64 MB
double fact(int n) {
double sum=1;
for (int j = 1; j <= n; j++) {
sum *= j;
}
return sum;
}
int main() {
int m, n;
if (scanf("%d %d", &m, &n) == 2) {
printf("result = %.0f", fact(n) / (fact(m) * fact(n - m)));
}
return 0;
}