第三题 编写函数fun,它的功能是:根据以下公式求P的值,结果由函数值带回。 m与n为两个正整数且要求m>n。p=m!/n!(m-n)!。例 如:m=12,n=8时,运行结果为495.000000。
时间: 2024-03-24 17:40:12 浏览: 41
好的,下面是实现该函数的代码:
```c
#include <stdio.h>
double fun(int m, int n) {
double p = 1.0;
for (int i = n + 1; i <= m; i++) {
p *= i;
}
for (int i = 1; i <= m - n; i++) {
p /= i;
}
return p;
}
int main() {
int m = 12;
int n = 8;
double p = fun(m, n);
printf("%f", p);
return 0;
}
```
在这个函数中,我们根据公式计算出 `p` 的值,并将结果返回。具体来说,我们首先将 `p` 的值初始化为 1,然后使用一个循环计算 `m` 的阶乘,即 `m * (m-1) * (m-2) * ... * (n+1)`;接着再使用一个循环计算 `(m-n)` 的阶乘,即 `(m-n) * (m-n-1) * (m-n-2) * ... * 1`,并将其除以 `p` 的值。最后将计算出的 `p` 的值返回。
在 `main` 函数中,我们调用 `fun` 函数并输出结果。