#include<stdio.h>
int fact (int n);
int main()
{
int n,m,result=0;
printf("Enter m:");
scanf("%d",&m);
printf("Enter n:");
scanf("%d",&n);
if(m>n)printf("输入错误");
else { n = fact(n); //求result = n!/(m!*(n-m)!);
m = fact(m);
result = n/(m*(n-m));
printf("result = %d",result); }
}
int fact (int n)
{
int i,sum=1;
for(i=1;i<=n;i++)
sum = sum*i;
return sum;
}
为什么输入
Enter m: 5
Enter n: 12
result = 0
?
你要求result = n!/(m!*(n-m)!)而不是result = n!/(m!*(n-m));
n-m的阶乘没求!
#include<stdio.h>
int fact (int n);
int main()
{
int n,m,k,result=0;
printf("Enter m:");
scanf("%d",&m);
printf("Enter n:");
scanf("%d",&n);
if(m>n)printf("输入错误");
else { n = fact(n); //求result = n!/(m!*(n-m)!);
m = fact(m);
k = fact(n-m);
result = n/(m*k);
printf("result = %d",result); }
}
int fact (int n)
{
int i,sum=1;
for(i=1;i<=n;i++)
sum = sum*i;
return sum;
}
嘻嘻
#include<stdio.h>
int fact (int n);
int main()
{
int n,m,k,result=0;
printf("Enter m:");
scanf("%d",&m);
printf("Enter n:");
scanf("%d",&n);
if(m>n)printf("输入错误");
else {
k=n-m;
n = fact(n); //求result = n!/(m!*(n-m)!);
m = fact(m);
result = n/(m*fact(k));
printf("result = %d\n",result); }
}
int fact (int n)
{
int i,sum=1;
for(i=1;i<=n;i++)
sum = sum*i;
return sum;
}