C语言作业题1

来源:百度知道 编辑:UC知道 时间:2024/06/06 04:01:07
Description

My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.

Input
One line

#include<stdio.h>
#define PI 3.1415926

int div(double s, double a[], int n)
{
int i, re = 0;
for(i = 0; i < n; i++)
re += (int)(a[i] * a[i] * PI / s);
return re;
}

int main()
{
int c, n, p, i;
double a[10010], sum, smax, min, max;
scanf("%d", &c);
while(c--)
{
scanf("%d %d", &n, &p);
p++;
sum = smax = 0.0;

for(i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
sum += PI * a[i] * a[i];
if(PI * a[i] * a[i] > smax)
smax = PI * a[i] * a[i];
}
min = smax / p / 2.0;
max = sum / p + 1;

while(1)
{
if(div(min, a, n) >= p && div(max, a, n) < p && max - min < 1e-6)
break;

if(div((max + min) / 2.0, a, n) < p)
max = (max + min) / 2.0;