int zuhe(int m, int n) {
if (n < 0 || n > m) return 0;
int sum = 1;
for (int i = 1; i <= n; i++) {// 计算 C(m, n) = m! / (n! * (m-n)!)
sum *= m - n + i;
sum /= i;
}
return sum;
}
方法1: C m n = A m n A n n = m ! n ! ( m ? n ) ! C_m^n=\frac{A_m^n}{A_n^n}=\frac{m!}{n!(m-n)!} Cmn?=Ann?Amn??=n!(m?n)!m!?
int jiecheng(int x){
if(x==1) return 1;
else return x*jiecheng(x-1);
}
int zuhe(int m,int n){
return jiecheng(m)/(jiecheng(n)*jiecheng(m-n));
}
方法2: C m n = C m ? 1 n ? 1 + C m ? 1 n C_m^n=C_{m-1}^{n-1}+C_{m-1}^n Cmn?=Cm?1n?1?+Cm?1n?
int zuhe(int m,int n){
if(m==n) return 1;
else if(n==1) return m;
else return zuhe(m-1,n-1)+zuhe(m-1,n);
}