c语言计算排列组合cnm
时间: 2023-12-02 14:38:14 浏览: 434
计算排列组合的公式为***!/((n-m)!m!),其中n>=m>=0。
下面是C语言计算排列组合的示例代码:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int permutation(int n, int m) {
return factorial(n) / factorial(n - m);
}
int combination(int n, int m) {
return factorial(n) / (factorial(n - m) * factorial(m));
}
int main() {
int n = 5, m = 3;
printf("A(%d,%d) = %d\n", n, m, permutation(n, m));
printf("C(%d,%d) = %d\n", n, m, combination(n, m));
return 0;
}
```
输出结果为:
```
A(5,3) = 60
C(5,3) = 10
```
阅读全文