Probably this is a little late, the 1e-9 is an epsilon, sometimes when you have a float number that should be "2" it gets a "1.999999999", if you aplly a floor to that number it will return a 1. the epsilon is there to complete the number and get a "2" as it should be.

Can someone please explain why the answer to this case is $0.02 and not $0.01? Seems like you only need to move one penny from the second person to the first person and then they are all within one penny of each other.

im gatting WA im sick of this problem...... here is my code.... i thought i was a easy probem.... plz help me
#include<iostream>
using namespace std;
double make2(double d)
{
long int a,e;
double b,c,f,g;
a=d;
b=d-a;
c=b*100;
e=c;
g=double(e*.01);
c=b*1000;
e=c;
if((e%1000)>4)
f=a+g+.01;
else
f=a+g;
return f;
}
int main()
{
double a[1000],average,dif,sum1,sum2;
long int i,n;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)&&n)
{
for(i=0;i<n;i++)
scanf("%lf",&a);
average=0;
for(i=0;i<n;i++)
average=average+a;
average=average/n;
average=make2(average);
sum1=0;
sum2=0;
for(i=0;i<n;i++)
{
if(a>average)
{
dif=a-average;
sum2=sum2+dif;
}
else
{
dif=average-a;
sum1=sum1+dif;
}
}
if(sum1<sum2)
printf("$%.2lf\n",sum1);
else
printf("$%.2lf\n",sum2);
}
return 0;
}
it solve all the case that i have found in website......
plz help me.....

PJYelton wrote:You are forgetting that sometimes the people who overpay need to give back more money than what the people who pay less need to receive. Like for example:

4 people, one pays $25.03, the other all pay $25. The average is $25.0075. Now all the people who pay less are within one cent, so your algorithm would return zero, but the person who paid more NEEDS to pay 2 cents or else he won't be within one cent, so the answer should be 2 cents.

Thanks... My mind sees only what is obvious to it...

HI Everyone!
I've been stucked in this problem for a while, the answer of the second input example always was 12.00 (instead of 11.99)
Today I finally could do something that the answer becomes 11.99 .
But I couldn't understand the algorithm correctly.
I tried to figure it out but I always reached 12.00

Please take a look and check where of my calculations is incorrect :
(Sorry if my English is terrible)

There are 4 persons.
Person 1 -> Spent 15.01
Person 2 -> Spent 15.00
Person 3 -> Spent 3.01
Person 4 -> Spent 3.00

What we should do is to calculate the average of all costs and then exchange money between these people so that everyone's costs reach the average.
The average is : 15.01 + 15.00 + 3.01 + 3.00 = 36.02
36.02 / 4 = 9.005

Now,
for Person 1 -> 15.01 - 9.005 = 6.005 It means that this person should GIVE 6.005 dollars to reach the average
for Person 2 -> 15.00 - 9.005 = 5.995 It means that this person should GIVE 5.995 dollars to reach the average
for Person 3 -> 9.005 - 3.01 = 5.995 It means that this person should TAKE 5.995 dollars to reach the average
for Person 4 -> 9.005 - 3.00 = 6.005 It means that this person should TAKE 6.005 dollars to reach the average

So the simplest thing ( the minimum money exchange) is that person 1 GIVE 6.005 to person 4 (who should TAKE 6.005)
and person 2 GIVE 5.995 to person 3 (who should TAKE 5.995)

So the minimum money exchange is : 5.995 + 6.005 = 12.00

I think I'm wrong in last part, I mean is there any other better way so this 4 people can exchange money?
Thank you ALL!

After the trip, each student's expenses are tallied and money is exchanged so that the net cost to each is the same, to within one cent. In the past, this money exchange has been tedious and time consuming. Your job is to compute, from a list of expenses, the minimum amount of money that must change hands in order to equalize (within a cent) all the students' costs.

Don't consider half cents. If person 1 gives person 4 $6, and person 2 gives person 3 $5.99, then they'll end up equalized within a cent.
1: $15.01 - $6.00 = $9.01
2: $15.00 - $5.99 = $9.01
3: $3.01 + $5.99 = $9.00
4: $3.00 + $6.00 = $9.00

Check input and AC output for thousands of problems on uDebug!

#include<iostream>
#define SIZE 1001
using namespace std;
int main()
{
double expenses[SIZE];
int num;
cin>>num;
while(num!=0)
{ double input=0;double sum=0.0f;
for(int i=0;i<num;i++)
{
cin>>input;
expenses=input;
sum += expenses; //total sum of expenditure in order to calculate mean
}
double mean=(sum)/num; //mean calculated here
double transfer=0.0f;

for(int i=0;i<num;i++)
{
if(expenses<mean) //all those expenses less then mean are substracted in order to calculate total transfer
transfer += mean-expenses;
}
cout<<'$'<<transfer<<endl;

cin>>num;
}
return 0;
}

but i am getting wring answer for this one.

If anyone can suggest me any reading material and correction possible in this code so that it runs properly.