C 语言程序计算 nPr 值?
排列,nPr 也可以表示为 P(n,r) 是一种用来求排列数的数学公式。P(n, r) 的公式为 n! / (n – r)!。
一个 n 元素集合的排列数为 n!,其中“!”表示阶乘。
Input:n=5;r=4; Output:120
说明
P(5, 4) = 5! / (5-4)! => 120 / 1 = 120 5!=1*2*3*4*5*=120
示例
#include<iostream>
using namespace std;
long int fact(int x) {
int i, f=1;
for(i=2; i<=x; i++) {
f=f*i;
}
return f;
}
int main() {
int n, r;
long int npr;
n=5;
r=4;
npr=fact(n)/fact(n-r);
printf("%d",npr);
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP