#include <stdio.h>
double computeChange(double money[],int length)
{
double result,average,sum,changeDown,changeUp;
int i;
result = average = sum = changeDown = changeUp = 0.00;
for(i=0; i<length; i++)
sum += money[i];
average = sum/length;
average = (long) (average * 100 + 0.5) / 100.00;
for (i=0; i<length; i++)
{
if(money[i] > average)
changeUp += (money[i] - average);
else
changeDown += (average - money[i]);
}
if (changeDown > 0 && changeUp > 0)
{
if(changeDown > changeUp)
result = changeUp;
else
result = changeDown;
}
else
{
if (changeUp == 0 && changeDown == 0)
result = 0.0;
else
{
if (changeDown == 0)
{
for (i = 0; i < length; i++)
if (money[i] > average)
result += (money[i] - average - 0.01);
}
else
{
for (i = 0; i < length; i++)
if (money[i] < average)
result += (average - money[i] - 0.01);
}
}
}
return result;
}
int main()
{
double money[1000] = {0};
int num;
int i;
while(scanf("%d",&num) && num!=0)
{
for(i=0; i<num; i++)
{
scanf("%lf",&money[i]);
}
printf("$%.2lf\n",computeChange(money,num));
}
return 0;
}
UVa 10137 The Trip
最新推荐文章于 2020-03-10 16:22:37 发布