题意:给出p,q,r,s,求c(p,q)/c(r,s)
分析:利用组合数公式,写出整体的分子和分母(用数组标记,分子分母各有哪些数),把相同的数字约去。然后把分子乘上,分母除掉。但是这样直接乘除会损失精度。所以我们定义double ans = 1;然后当ans<1时算乘法,ans>=1时算除法,以控制ans始终在1不会和1相差10000倍以上,以保证精度。
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 10005
int a, b, c, d;
int up[maxn], down[maxn];
int n;
void make(int m, int n, int up[], int down[])
{
if (n - m < m)
m = n - m;
for (int i = n; i > n - m; i--)
up[i]++;
for (int i = 1; i <= m; i++)
down[i]++;
}
void work()
{
n = max(max(a, b), max(c, d));
for (int i = 0; i <= n; i++)
{
int x = min(up[i], down[i]);
up[i] -= x;
down[i] -= x;
}
int p = 1, q = 1;
while (p <= n && !up[p])
p++;
while (q <= n && !down[q])
q++;
double ans = 1;
while (p <= n || q <= n)
{
if ((ans <= 1 && p <= n) || q > n)
{
ans *= p;
up[p]--;
while (p <= n && !up[p])
p++;
}
else
{
ans /= q;
down[q]--;
while (q <= n && !down[q])
q++;
}
}
printf("%.5f\n", ans);
}
int main()
{
//freopen("t.txt", "r", stdin);
while (~scanf("%d%d%d%d", &a, &b, &c, &d))
{
memset(up, 0, sizeof(up));
memset(down, 0, sizeof(down));
make(b, a, up, down);
make(d, c, down, up);
work();
}
return 0;
}