zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    软件工程概论总结第三章
    软件工程概论总结第二章
    软件工程概论总结
    软件工程概论10-软件测试
    软件工程概论9-软件实现
    软件工程概论-8面向对象设计
    软件工程概论-7面向对象分析
    软件工程概论-6面向对象基础
    软件工程概论-5软件工程中的形式化方法
    软件工程概论-4需求过程
  • 原文地址:https://www.cnblogs.com/rainydays/p/2198538.html
Copyright © 2011-2022 走看看