UVA 10375 (13.11.07)
Problem D: Choose and divideThe 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-negativeinteger 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 numberwith 5 digits of precision in the fraction, giving the number asdescribed above. You may assume the result is not greater than100,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
题意我就不说了~
错误思路; 首先我用了先全部乘起来后一起除的做法, 结果超出数据范围~, 所以这条路走不通了
正确做法: 因为这个式子分子分母的项数一样, 我们可采取乘一次除一次的方式, 不仅如此, 两个函数还能一起处理~
AC代码:
#include<stdio.h> double getSum(double m, double n, double x, double y) { double sum = 1; double l1, l2; if(m - n < n) l1 = m - n; else l1 = n; if(x - y < y) l2 = x - y; else l2 = y; for(int i = 1; i <= l1 || i <= l2; i++) { if(i <= l1) sum = sum * (m - l1 + i) / i; if(i <= l2) sum = sum * i / (x - l2 + i); } return sum; } int main() { double p, q, r, s; while(scanf("%lf %lf %lf %lf", &p, &q, &r, &s) != EOF) printf("%.5lf\n", getSum(p, q, r, s)); return 0; }
补充:软件开发 , C++ ,