编程题 编写函数fun,它的功能是:根据以下公式求p的值,结果由函数值带回。M与n为两个正整数且要求m>n。
P=
例如:m=12,n=8时,运行结果为495.000000
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include <conio.h>
#include <stdio.h>
float fun( int m, int n)
{
}
main()
{
FILE *out;
printf("P=%f\n", fun(12,8));
out = fopen("out.dat", "w");
fprintf(out, "%f", fun(12,6));
fclose(out);
}
答案是: float fun(int m,int n)
{
float p,t=1.0;
int i;
for (i=1;i<=m;i++)
t=t*i;
p=t;
for(t=1.0,i=1;i<=n;i++)
t=t*i;
p=p/t;
for(t=1.0,i=1;i<=m-n;i++)
t=t*i;
p=p/t;
return p;
}