编写求n!的函数fint n),在主函数中输 入两个整数m、口,然后调用函数求:C=m!/n!(m-n)!
时间: 2024-02-25 08:59:43 浏览: 45
好的,以下是代码实现:
```c
#include <stdio.h>
int fact(int n) {
if (n == 0 || n == 1) {
return 1;
}
else {
return n * fact(n-1);
}
}
int main() {
int m, n;
printf("请输入两个整数m和n:");
scanf("%d %d", &m, &n);
int c = fact(m) / (fact(n) * fact(m-n));
printf("C = %d\n", c);
return 0;
}
```
首先,我们定义了一个求阶乘的函数 `fact`,采用递归的方式实现。然后在主函数中,输入两个整数 `m` 和 `n`,调用 `fact` 函数求出 `m!`、`n!` 和 `(m-n)!`,最终计算并输出组合数 `C`。
相关问题
#include <stdio.h> void num () (exter int x, y; inta=15, b=10: ×=a-b:y=atb; int X, V: int main () fint a=7, b=5: x=a+b; y-a-b: num () : printf ("%d, %dn" x,y) return (0)
这段代码存在语法错误,需要做如下修改:
```
#include <stdio.h>
void num() {
extern int x, y;
int a=15, b=10;
x=a-b;
y=a*b;
}
int main() {
int a=7, b=5;
int x=a+b;
int y=a-b;
num();
printf("%d, %d\n", x, y);
return (0);
}
```
这段代码的功能是定义了一个函数 `num()` 和一个主函数 `main()`,并在主函数中调用了 `num()` 函数。`num()` 函数中定义了两个全局变量 `x` 和 `y`,并且通过外部变量声明关键字 `extern` 声明了这两个变量在其他地方已经定义。 `num()` 函数中计算了 `x=a-b` 和 `y=a*b` 的值。主函数中定义了两个整型变量 `a` 和 `b`,并计算了 `x=a+b` 和 `y=a-b` 的值。在主函数中调用 `num()` 函数,并通过 `printf` 函数输出了 `x` 和 `y` 的值。
C语言若x、y取值为区间[16]的整数,显示使函数f(xv)取最小值的x1、v1。 函数f的原型为double fint.int)若x,y取值为区间[1.6]的整数,显示使函数f(xv)取最小值的x1. y1。函数f的原型为double f(int,int) f(x, y)= 3.14*x-y x+y
这个问题和之前的问题很相似,只是多了一个函数f的原型。我们可以这样实现:
```c
#include <stdio.h>
double f(int x, int y) {
return 3.14*x - y + x + y;
}
int main() {
FILE *fp;
double min = f(1, 1);
int x1 = 1, y1 = 1;
for (int i = 1; i <= 16; i++) {
for (int j = 1; j <= 16; j++) {
double tmp = f(i, j);
if (tmp < min) {
min = tmp;
x1 = i;
y1 = j;
}
}
}
printf("%f %d %d\n", min, x1, y1);
fp = fopen("CD2.dat", "wb");
fwrite(&min, 8, 1, fp);
fclose(fp);
return 0;
}
```
其中,函数f的实现和之前一样,双重循环枚举区间[16]内的整数x和y,计算函数f(x,y)的值并与当前最小值进行比较,如果更小就更新最小值和对应的x1、y1值。最后输出结果并将最小值写入二进制文件CD2.dat中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)