poj2613

题意:给出p,q,r,s,求c(p,q)/c(r,s)

分析:利用组合数公式,写出整体的分子和分母(用数组标记,分子分母各有哪些数),把相同的数字约去。然后把分子乘上,分母除掉。但是这样直接乘除会损失精度。所以我们定义double ans = 1;然后当ans<1时算乘法,ans>=1时算除法,以控制ans始终在1不会和1相差10000倍以上,以保证精度。

ContractedBlock.gif ExpandedBlockStart.gif View Code
#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值