阶乘相除,一定要一乘一除地算,不能分子分母全算完之后再除一把,这样绝对溢出,测试数据很大很全,偷懒的办法是不行的。。
#include<cstdio>
#include<deque>
#include<algorithm>
using namespace std;
int main()
{
int p, q, r, s;
while (scanf("%d%d%d%d", &p, &q, &r, &s) != EOF)
{
q = min(q, p - q);
s = min(s, r - s);
deque<int> up, down;
for (int i = p; i > p - q; i--)
up.push_back(i);
for (int i = r; i > r - s; i--)
down.push_back(i);
if (s > q)
for (int i = s; i > q; i--)
up.push_back(i);
else
for (int i = q; i > s; i--)
down.push_back(i);
double res = 1;
for (size_t i = 0; i != up.size(); i++)
{
res *= up[i];
if (!down.empty())
{
res /= down.front();
down.pop_front();
}
}
while (!down.empty())
{
res /= down.front();
down.pop_front();
}
printf("%.5lf\n", res);
}
return 0;
}