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

This requires programming problem, according to the formula C n m = m ! ( N - m ) ! N ! Is calculated from the extraction of n different elements of m elements ( m n) of the 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:

The input gives two positive integers m and n ( m n) on one line, separated by spaces.

Output format:

Output according to the format "result = calculation result of number of combinations". The title guarantees that the result is within the doublecategory.

Sample input:

2 7
 

Sample output:

result = 21











#include<stdio.h>
int main ()
{
    double fact(int n);
    int m,n;
    double sum=0;
    scanf("%d%d",&m,&n);
    if(m<=n)
    {
        sum=1.0*fact(n)/(fact(m)*fact(n-m));
        printf("result = %.0lf",sum);
        
    }else
    {
        scanf("%d%d",&m,&n);
    }
    
    return 0;
}
double fact(int n)
{
    int i;
    double sum=1;
    for(i=1;i<=n;i++)
    {
        sum*=i;
    }
    return sum;

}
    

Guess you like

Origin www.cnblogs.com/wven/p/12680921.html