题意:求两个组合的商,利用数学阶乘的概念还是蛮简单的,
m! C(m,n) = -------- n!(m-n)! 例如这个,先找到n与(m-n)之间的较小值,然后式子就等于(p*...*(p-Min+1))/Max 接下来就是简单的除法了#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; double cal(int p,int q,int r,int s){ double cnt = 1; q = min(q,p-q); s = min(s,r-s); p = p-q+1,r=r-s+1; int i = 1,j = 1; while (i <= q || j <= s){ if (i <= q){ cnt = cnt*p/i; p++,i++; } if (j <= s){ cnt = cnt/r*j; r++,j++; } } return cnt; } int main(){ int p,q,r,s; while (scanf("%d%d%d%d",&p,&q,&r,&s) != EOF){ double ans = cal(p,q,r,s); printf("%.5lf\n",ans); } return 0; }