This question requires writing a program, according to the formula
C n m = m! (N − m)! N! C_n^{m} =\frac{m!}{(n−m)!n !}Cnm=(n−m)!n!m!
Calculating removed from the n different elements of the m
element (m≤n)
number of combinations.
It is recommended to define and call function fact(n)
calculations n!
, where n
the type is int
and the function type is double
.
Input format:
Enter two positive integers on one line, m和n(m≤n)
separated by spaces.
Output format:
result = 组合数计算结果
Output in the format " ". The question guarantees that the result is within the double
range of the type.
Input sample:
2 7
Sample output:
result = 21
Code:
# include <stdio.h>
# include <stdlib.h>
double fact(int q) {
int i;
double value = 1;
for (i=1;i<=q;i++) {
value *= i;
}
return value;
}
int main() {
int m,n;
scanf("%d %d",&m,&n);
double result = fact(n) / (fact(m) * fact(n-m));
printf("result = %.0lf",result);
return 0;
}
Submit screenshot:
Problem-solving ideas:
It’s a good time to talk about this question, classmates, if the situation when your code is submitted is the same as the following:
And it keeps showing you m == n/2 测试点2未通过
, congratulations, you only need to change the type double fact(int n)
of the variable that receives the value in the value
achievement double
, don’t ask me how to know, Asking is gifted