Re: 10137 - The Trip, get WA
Posted: Tue Feb 18, 2014 9:52 pm
Try solving it without using floating point.
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;
}
brianfry713 wrote:brianfry713 wrote:brianfry713 wrote:Try solving it without using floating point.
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;
}
Don't use double, only use integers.brianfry713 wrote:Try solving it without using floating point.
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);
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........
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;
}
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