UVa 10375 Choose and divide (組合數相除)

來源:互聯網
上載者:User

10375 - Choose and divide

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1316

The binomial coefficient C(m,n) is defined as

        m!

C(m,n) = --------

        n!(m-n)!

Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s).

The Input

Input consists of a sequence of lines. Each line contains four non-negative integer numbers giving values for p, q, r, and s, respectively, separated by a single space. All the numbers will be smaller than 10,000 with p>=q and r>=s.

The Output

For each line of input, print a single line containing a real number with 5 digits of precision in the fraction, giving the number as described above. You may assume the result is not greater than 100,000,000.

Sample Input

10 5 14 9

93 45 84 59

145 95 143 92

995 487 996 488

2000 1000 1999 999

9998 4999 9996 4998

Output for Sample Input

0.12587

505606.46055

1.28223

0.48996

2.00000

3.99960

思路:不要被題目那個公式誤導了~自己算一算幾個組合數,比如C(100,4),發現分子分母都是4個乘數,那就好辦了,寫一個for迴圈每次一除一乘就是。

完整代碼:

/*0.022s*/    #include<cstdio>  #include<algorithm>  using namespace std;      int main()  {      int p, q, r, s,maxn,i;      double ans;      while (~scanf("%d%d%d%d", &p, &q, &r, &s))      {          q = max(q, p - q), s = max(s, r - s);          maxn = max(q, s);          ans = 1.0;          for (i = 1; i <= maxn; ++i)          {              if (i <= q) ans = ans / i * (p - q + i);              if (i <= s) ans = ans / (r - s + i) * i;          }          printf("%.5f\n", ans);      }      return 0;  }

作者署名:csdn部落格 synapse7

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.