Experiment 2-4-7 Find the number of combinations (15 points)

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=(nm)!n!m!

Calculating removed from the n different elements of the melement (m≤n)number of combinations.

It is recommended to define and call function fact(n)calculations n!, where nthe type is intand 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 doublerange 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:

Insert picture description here

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:
Insert picture description here
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 valueachievement double, don’t ask me how to know, Asking is gifted

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114434784