POJ 3122

这道题wa了很多次,原因是PI的精度问题,精度一定很大

#include <stdio.h>
#define maxn 100010
#define PI 3.14159265358979323846 
int main()
{
	int amount;
	scanf("%d",&amount);
	while(amount--)
	{
		int n,f;
		scanf("%d%d",&n,&f);
		int i;
		double pie;
		double s[maxn];
		double high=0;
		double low=0;
		for(i=0;i<n;i++)
		{
			scanf("%lf",&pie);
			s[i]=PI*pie*pie;
			if(s[i]>high)
				high=s[i];//因为每个人分的蛋糕必须是一整的
		}
		while((high-low)>1e-6)
		{
			int sum=0;
			double mid=(high+low)/2;
			for(i=0;i<n;i++)
			{
				sum+=(int)(s[i]/mid);
			}
			if(sum>=(f+1)) low=mid;
			else high=mid;
		}
		printf("%.4lf\n",low);
	}
	return 0;
}


 

原文地址:https://www.cnblogs.com/lj030/p/3002184.html