此题注意点:1.人数不要忘记还有你自己,记得+1;2.pi用arccos(-1.0)求会好点;3.精度问题,
在二分查找while比较时用right-left>1e-5以上,在验证数量时(int)((体积+1e-5)/v)
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define pi acos(-1.0)
int pie[10010];
double spie[10010];
int n,f,m;
bool Judge(double s)
{
int num,i;
num=0;
for(i=n-1;i>=0;i--)
{
int temp=(int)((spie[i]+1e-5)/s);
num+=temp;
}
if(num>=m)
return true;
else
return false;
}
double Binsearch(double left,double right)
{
double mid;
while(right-left>1e-6)
{
mid=(left+right)/2;
if(Judge(mid))
{
left=mid;
}
else
right=mid;
}
return left;
}
int main()
{
int i,T;
double low,high;
double v;
while(scanf("%d",&T)!=EOF)
{
while(T--)
{
memset(pie,0,sizeof(pie));
memset(spie,0,sizeof(spie));
scanf("%d%d",&n,&f);
m=f+1;
for(i=0;i<n;i++)
{
scanf("%d",&pie[i]);
spie[i]=pie[i]*pie[i]*pi;
}
sort(spie,spie+n);
low=0;
high=spie[n-1];
// if(m<=n) n=m;
v=Binsearch(low,high);
printf("%.4lf\n",v);
}//T--
}//EOF
return 0;
}