10137 - The Trip
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10137 - The Trip, get WA
Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!
Re: 10137 - The Trip, get WA
I used int
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10137 - The Trip, get WA
This line uses floating point:
printf("$%.2f\n", ((give > take) ? give : take) / 100.0f);
printf("$%.2f\n", ((give > take) ? give : take) / 100.0f);
Check input and AC output for thousands of problems on uDebug!
Re: 10137 - The Trip, get WA
Thank you. This is the point.
Re: 10137 - The Trip
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10137 - The Trip
brianfry713 wrote:brianfry713 wrote:brianfry713 wrote:Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!
Re: 10137 - The Trip
I have modified the program without using floating point. But same problem.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10137 - The Trip
Post your updated code.
Check input and AC output for thousands of problems on uDebug!
Re: 10137 - The Trip
Hi...Here is my updated code.brianfry713 wrote:Post your 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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10137 - The Trip
Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!
Re: 10137 - The Trip
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;
}
#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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10137 - The Trip
Don't use double, only use integers.brianfry713 wrote:Try solving it without using floating point.
Check input and AC output for thousands of problems on uDebug!
-
- Experienced poster
- Posts: 122
- Joined: Tue Apr 16, 2002 10:07 am
Re: 10137 - The Trip
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:
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
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, ¢sPart);
expensesInCents[i] = (dollarPart * 100) + centsPart;
totalExpenses += expensesInCents[i];
}
averageExpense = (totalExpenses / n);
residue = (totalExpenses % n);
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
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
hey guys !
again help please ::
my code as follows
it passed all the tests in this forums
the problem is with the test
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
again help please ::
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;
}
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
but AC output is 0.00 -_- did i miss sth
or there is a problem with the judge