
在C编程语言中,nCr被称为组合。 nCr 是从 n 个对象的集合中选择 r 个对象,其中对象的顺序并不重要。
nPr 称为排列 。 nPr 是一组“n”个对象中“r”个对象的排列,这些对象应该按顺序或序列排列。
求排列的公式以及 C 语言中给定数字的组合如下 -
求 nCr 的逻辑如下 -
result = factorial(n)/(factorial(r)*factorial(n-r));
找到nPr的逻辑如下 −
result = factorial(n)/factorial(n-r);
以下是用于找到给定数字的排列和组合的C程序−
#include <stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
int main(){
int n, r;
long ncr, npr;
printf("Enter the value of n and r</p><p>");
scanf("%d%d",&n,&r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld</p><p>", n, r, ncr);
printf("%dP%d = %ld</p><p>", n, r, npr);
return 0;
}
long find_ncr(int n, int r) {
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long find_npr(int n, int r) {
long result;
result = factorial(n)/factorial(n-r);
return result;
}
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}执行上述程序时,会产生以下输出 -
Enter the value of n and r 5 2 5C2 = 10 5P2 = 20
以上就是C程序找到nCr和nPr的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号