题目链接:http://poj.org/problem?id=3122
用二分法找到最大的馅饼的面积
代码:
<span style="font-size:18px;">#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 10010;
double r[maxn];
const double pi=3.14159265359;
const double esp=1e-6;
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, f;
scanf("%d%d", &n, &f);
f++;
double maxt = -1;
for(int i = 1; i<= n; i++)
{
scanf("%lf", &r[i]);
r[i] *= r[i];
if(r[i]>maxt)
{
maxt = r[i];
}
}
double low = 0;
double mid = 0;
while(maxt - low > esp)
{
mid = (low + maxt) / 2;
int cnt = 0;
for(int i = 1; i<= n; i++)
{
cnt += (int)r[i] / mid;
}
if(cnt < f)
{
maxt = mid;
}
else
{
low = mid;
}
}
printf("%.4f\n", mid*pi);
}
return 0;
}</span>