Page 10 of 11

### Re: 10137 - The Trip, get WA

Posted: Tue Feb 18, 2014 9:52 pm
Try solving it without using floating point.

### Re: 10137 - The Trip, get WA

Posted: Wed Feb 19, 2014 3:51 pm
I used int

### Re: 10137 - The Trip, get WA

Posted: Wed Feb 19, 2014 9:28 pm
This line uses floating point:
printf("\$%.2f\n", ((give > take) ? give : take) / 100.0f);

### Re: 10137 - The Trip, get WA

Posted: Thu Feb 20, 2014 11:00 am
Thank you. This is the point.

### Re: 10137 - The Trip

Posted: Tue Mar 11, 2014 8:15 am
Hi...I don't know why i m getting error...i have tried several inputs and those work...any hints experts?

Code: Select all

``````
#include<iostream>
#include<math.h>
#include<stdio.h>

using namespace std;

int main()
{
long input;
double buck;
double average = 0.00;
double positivesum = 0.00;
double negativesum = 0.00;
double track;
double* arr;

while(cin >> input && input != 0)
{
arr = new double[input];
for(int i = 0; i < input; i++)
{
cin >> buck;
if(buck >= 0.00)
{
average = average + buck;
arr[i] = buck;
}
}
average = average/double(input);

//double rounded_down = floorf(average * 100) / 100;   /* Result: 37.77 */
double nearest = floorf(average * 100 + 0.5) / 100;  /* Result: 37.78 */
//double rounded_up = ceilf(average * 100) / 100;      /* Result: 37.78 */

for(int i = 0; i < input; i++)
{
if(arr[i] > average)
{
positivesum += (arr[i]-nearest);
}
else
{
negativesum += (nearest-arr[i]);
}
}
if(positivesum < negativesum)
{
printf("\$");
printf("%0.2lf\n",positivesum);
}
else
{
printf("\$");
printf("%0.2lf\n",negativesum);
}
positivesum = 0.00;
negativesum = 0.00;
average = 0.00;
}
return 0;
}

``````

### Re: 10137 - The Trip

Posted: Tue Mar 11, 2014 8:03 pm
brianfry713 wrote:
brianfry713 wrote:
brianfry713 wrote:Try solving it without using floating point.

### Re: 10137 - The Trip

Posted: Tue Mar 11, 2014 8:26 pm
I have modified the program without using floating point. But same problem.

### Re: 10137 - The Trip

Posted: Wed Mar 12, 2014 8:26 pm

### Re: 10137 - The Trip

Posted: Thu Mar 13, 2014 4:11 am
Hi...Here is my updated code.

Code: Select all

``````
#include<iostream>
#include<math.h>
#include<stdio.h>

using namespace std;

int main()
{
long input;
double buck;
double average = 0;
double positivesum = 0;
double negativesum = 0;
double track;
double* arr;

while(cin >> input && input != 0)
{
arr = new double[input];
for(int i = 0; i < input; i++)
{
cin >> buck;
if(buck >= 0)
{
average = average + buck;
arr[i] = buck;
}
}
average = average/input;
double nearest = floorf(average * 100.0 + 0.5) / 100.0;
for(int i = 0; i < input; i++)
{
if(arr[i] > average)
{
positivesum += (arr[i]-nearest);
}
else
{
negativesum += (nearest-arr[i]);
}
}
if(positivesum < negativesum)
{
printf("\$%0.2lf\n",positivesum);
}
else
{
printf("\$%0.2lf\n",negativesum);
}
positivesum = 0;
negativesum = 0;
average = 0;
}
return 0;
}
``````

### Re: 10137 - The Trip

Posted: Thu Mar 13, 2014 8:24 pm
Try solving it without using floating point.

### Re: 10137 - The Trip

Posted: Sun Mar 23, 2014 6:02 am
Hi...I have changed my code little bit..but still WA....can you please give me some test input?I am sure i am getting WA for formatting..

#include<iostream>
#include<math.h>
#include<stdio.h>

using namespace std;

int main()
{
long input;
double buck;
double average = 0;
double positivesum = 0;
double negativesum = 0;
double* arr;

while(cin >> input && input != 0)
{
arr = new double[input];
for(int i = 0; i < input; i++)
{
cin >> buck;
if(buck >= 0)
{
average = average + buck;
arr = buck;
}
}
average = average/input;

double nearest = floorf(average * 100 + 0.5) / 100;

for(int i = 0; i < input; i++)
{
if(arr > average)
{
positivesum += (arr-nearest);
}
else
{
negativesum += (nearest-arr);
}
}
if(positivesum < negativesum)
{
printf("\$");
printf("%0.2lf\n",positivesum);
}
else
{
printf("\$");
printf("%0.2lf\n",negativesum);
}
positivesum = 0;
negativesum = 0;
average = 0;
}
return 0;
}

### Re: 10137 - The Trip

Posted: Tue Mar 25, 2014 12:12 am
brianfry713 wrote:Try solving it without using floating point.
Don't use double, only use integers.

### Re: 10137 - The Trip

Posted: Mon Aug 11, 2014 6:28 pm
I just got AC for this problem and wanted to share a small tip.

I solved the problem using whole numbers because I am not confident about floating points for this problem. To take input, I used the following code:

Code: Select all

``````for (int i = 0; i < n; i++)
{
scanf("%ld.%ld", &dollarPart, &centsPart);
expensesInCents[i] = (dollarPart * 100) + centsPart;
totalExpenses += expensesInCents[i];
}

averageExpense = (totalExpenses / n);
residue = (totalExpenses % n);
``````
Then, for each person who spent more than averageExpense, they should be re-imbursed.
Each unit is

"averageExpense" is the MINIMUM amount that should be spent by each person.
Each unit from "residue" is then given to the first persons who spent more than the average.

Example:
Expenses = \$1.02
Expenses in cents = 102 cents
No. of people = 5
AverageExpense = 20 cents
Residue = 2

People 1 should spend 20 + 1
People 2 should spend 20 + 1
People 3 should spend 20
People 4 should spend 20
People 5 should spend 20

### Re: 10137 - The Trip

Posted: Sat Sep 06, 2014 6:28 pm
for (int i = 0; i < numPpl; i++) {
cin >> temp;
avg += (int) (temp * 10000 + 1e-9) / numPpl;
cents = (int) (temp * 100 + 1e-9);
top = max(top, cents);

Can someone explain " + 1e-9 " ???

thx a lot........

take it as a habit and if you want to compare to reals
use
abs(x-y) <= 1e-9

### Re: 10137 - The Trip

Posted: Sat Sep 06, 2014 8:28 pm
hey guys !
my code as follows

Code: Select all

``````#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
long long n;
while(cin >> n && n != 0)
{
long long *exp = new long long[n];
long long avg = 0, res;

for(long long i = 0; i<n; i++)
{
double x;
cin>>x;
x += 1e-9;
exp[i] = 100*x;
avg += exp[i];
}
res = avg%n;
avg /= n;
long long count1 = 0, count2 = 0;
long long pos = 0, neg = 0;
for(long long i = 0; i <n; i++)
{
long long cash = exp[i] - avg;
if(cash > 0)
{
pos += cash;
count1++;
}
else
{
neg -= cash;
count2++;
}
}
long long res1 = res;
for(long long i = 1; i<=count1 && res>0; i++ , res--)
{
pos--;
}
for(long long i = 1; i<=count2 && res1>0; i++ , res1--)
{
neg++;
}
printf("\$%.2f\n" , ((pos < neg)? pos : neg) / 100.0f);
}
return 0;
}
``````
it passed all the tests in this forums
the problem is with the test

Code: Select all

``````15
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.03
``````
the output is clearly 0.01 as my approach
but AC output is 0.00 -_- did i miss sth
or there is a problem with the judge