• poj2613


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

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

    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;
    }

  • 相关阅读:
    安装项目管理工具 SVN+Redmine
    jquery validate
    NHibernate集合映射中的set, list, map, bag, array
    NHibernate执行原始SQL代码的方法小结 .
    一个简单的存储过程
    修改Project中的表名及字段名
    用代码修改类名
    实现Pick和Reigister
    转移单的装运和收货
    库存维度检查
  • 原文地址:https://www.cnblogs.com/rainydays/p/2198538.html
Copyright © 2020-2023  润新知