Page 2 of 11

Got it here, too

Posted: Wed Jun 11, 2003 5:54 am
by xidao
Yo, ggll, good sample data. I posted on other threads, including one of my onw, about my frustrations with this problem, but after using that sample data I was able to change one line and life was good again.

Thanks.

10137 help plz

Posted: Sat Jun 21, 2003 2:20 am
by powerboy
I just don't see what is wrong with my program.
I converted everything into cents.
I then found the average of these costs in cents rounding down.
I then found the remaining cents which need to be distributed using
remainder = sum mod # of trips.
I counted the # of costs larger than the average, denote by count.
I then added all the difference between the average and the costs
smaller than the average.
This total of difference + remainder - count should equal
the total exchanged amount.

what is wrong with my program????
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
int n;
int *array;
int sum;

while (cin >> n)
{
if (n == 0)
return 0;

sum = 0;

array = new int[n];

int i;

double temp;
for (i = 0; i < n; i++)
{
cin >> temp;
array = (int) (temp * 100);
sum += array;
}

int myAverage = sum / n;
int remainder = sum % n;

int mySum = 0;

int count = 0;
for (i = 0; i < n; i++)
{
if (myAverage > array)
{
mySum += myAverage - array;
}
else if (array < myAverage)
{
count++;
}
}

if (remainder != 0)
mySum += remainder - count;

double tempSum = mySum/(100.0);
printf("$%.2f\n", tempSum);
delete array;
}
}

Posted: Sat Jul 05, 2003 6:40 am
by carneiro
you are forgetting the cases in which there are more costs above the average than below (or vice-versa)

Posted: Sat Jul 05, 2003 2:43 pm
by powerboy
Hi, I did take the case into consideration.
Can you give a testing case?

Posted: Sat Jul 05, 2003 5:44 pm
by carneiro
maybe this one :

3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
5
5000.00
11.11
11.11
11.11
11.11
3
0.01
0.03
0.03
4
25.00
25.00
25.00
28.00
3
10.01
15.25
18.96
4
25.03
25.00
25.00
25.00
0


Output for this should be:

$10.00
$11.99
$3991.11
$0.01
$2.25
$4.73
$0.02

Posted: Sat Jul 05, 2003 9:39 pm
by powerboy
Hey, Mauricio
I did the test cases you provided. Thank you.
The answers are the same as the ones you provided.
So i still don't see anything wrong with it.

Posted: Sat Jul 12, 2003 1:30 am
by ggll
The variable count will always be 0, can you see why?

Most likely there is also a double precision issue. Try adding an EPS to the int cast.

Looks like these two bugs together get you passed the test cases above.

10137 HELPPPPPPPPPPPPP!!!!

Posted: Sat Jul 26, 2003 9:39 am
by waseem50
hi guys ive beeen struggining with the trip, but am getting a wa
my approach is simple, i find the average ,round it mathematically ie <0.5 =0, >0.5 =1 , then i find the sum of extra pennies, and the sum of the penny deficit ie sum of pennies bigger and smaller than average, then i output the smaller of the 2, but still am getting a WA.
:-?
this is my code with some input and output!!!

input
3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
5
5000.00
11.11
11.11
11.11
11.11
3
0.01
0.03
0.03
4
25.00
25.00
25.00
28.00
3
10.01
15.25
18.96
4
25.03
25.00
25.00
25.00
0

output
$10.00
$11.99
$3991.11
$0.01
$2.25
$4.73
$0.02
#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
using namespace std;


double round(double x, int precision);

int main()
{
#ifndef ONLINE_JUDGE
ifstream cin("input.txt",ios::in);
#endif

int number,i;
cout.setf(ios::showpoint|ios::fixed);

for(cin>>number,cin.ignore();number!=0;cin>>number,cin.ignore())
{
float sum=0;

float *arr=new float[number];
for(i=0;i<number;i++)
{
cin>>arr;
cin.ignore();
}
for(i=0;i<number;i++)
sum+=(arr);
float average=round((sum/number),2);



float result1=0;
float result2=0;
for(i=0;i<number;i++)/*finds the smallest from the average*/
if(average-arr>0) result1+=average-arr;
else result2+=arr-average;
if (result1==0)
result1=result2;
if (result2==0)
result2=result1;
float result=(result1<result2)?result1:result2;

cout<<setprecision(2)<<"$"<<result<<endl;
}

return 0;
}

double round(double x, int precision)
{

return floor(x*pow(10.0,(double)precision)+0.5)/pow(10.0,(double)precision);
}

Solved 10137

Posted: Sun Jul 27, 2003 11:21 pm
by waseem50
Hey everybody i read in one of the posts to uses long long, so i replaced all my floats and doubles with long double, and i got AC immeduitely without anu other modification to my code :lol: :lol: :lol: :lol: [/cpp]

10137 Why WA ?

Posted: Thu Sep 04, 2003 10:24 am
by zilnus
[cpp]
#include <iostream.h>

int main()
{
int a = 1,i;
long double amount[1000];
long double temp,total,rata,hasil;

while (a != 0)
{
cin >> a;
total = hasil = 0;
if (a != 0)
{
for (i=0;i<a;i++) {
cin >> temp;
amount = temp;
}

for (i=0;i<a;i++)
total += amount;
rata = total/a;
rata = rata*100;
rata = (int)rata;
rata = rata/100;

for (i=0;i<a;i++){
if (amount < rata) hasil = hasil + (rata-amount);
}
cout << "$" << hasil << endl;
}
}
return 0;
}[/cpp]

Why Wrong answer ? Is there test case that make my source code not valid ?[/c]

Posted: Sat Sep 06, 2003 1:56 am
by gvcormac
21
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.01
0.01
0.01
0.01
0.01
0.01
0.03
0


Your answer is

$0

Which is the wrong amount, and is missing the .00

10137 - The Trip

Posted: Sat Oct 04, 2003 9:07 am
by Eva
I don't know! This is not giving me the sample 2nd output!
But why?

This should be the correct answer! [cpp]#include<iostream.h>
#include<iomanip.h>

int main()
{
int n = 0;

while(cin >> n)
{
if(n == 0)
return 0;

double trip[1000] = {0};
double sum = 0,ans = 0;

for(int i=0;i<n;++i)
{
cin >> trip;
sum += trip;
}

double avg = sum/double(n);

for(int i=0;i<n;++i)
if(trip > avg)
ans += trip-avg;

cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(2);

cout << '$' << ans << endl;
}

return 0;
}[/cpp]

Posted: Sat Oct 04, 2003 6:54 pm
by Eva
oh! It gives 12.00 but the sample output says 11.99 why :x :x

Posted: Wed Oct 08, 2003 12:29 am
by saintp
The problem lies in the fact that you can be one penny off. So where you do

Code: Select all

ans += trip[i] - avg;
You're only measuring the distance from those people who spent more than the average. In fact, those people who spent *less* might be closer, and in this case, they are. You need to compute both the total spent over the average and the total spent under, compare the two, and return the lesser of the two.

Posted: Wed Oct 08, 2003 3:53 am
by saintp
Incidentally, I'm having problems with mine too, of the more ineffable variety. It passes all the tests I throw at it, but I still get WA. Any help would be much appreciated:

Code: Select all

/*  @JUDGE_ID:  36781AT 10137 C++ */
#include <iostream>
using namespace std;

int min(int num1, int num2) {
  if (num1 < num2) {
    return num1;
  } else {
    return num2;
  }
}

int max(int num1, int num2) {
  if (num1 > num2) {
    return num1;
  } else {
    return num2;
  }
}

int calcBest(int cents[], int avg, int numPpl) {
  int top = 0, bottom = 0, middle = 0;
  for (int i = 0; i < numPpl; i++) {
    if (cents[i] > avg) {
      top += cents[i] - avg;
    } else if (cents[i] < avg) {
      bottom += avg - cents[i];
    }
  }
  if (top == 0) {
    return calcBest(cents, --avg, numPpl);
  } else if (bottom == 0) {
    return calcBest(cents, ++avg, numPpl);
  }
  return min(top, bottom);
}

int main() {
  int cents[1000];
  int numPpl, avg, best, top, bottom;
  double temp;
  while ((cin >> numPpl) && (numPpl != 0)) {
    top = avg = 0;
    bottom = 10000000;
    for (int i = 0; i < numPpl; i++) {
      cin >> temp;
      avg += (int) (temp * 10000 + 1e-9) / numPpl;
      cents[i] = (int) (temp * 100 + 1e-9);
      top = max(top, cents[i]);
      bottom = min(bottom, cents[i]);
    }
    if (abs(top - bottom) <= 1) {
      best = 0;
    } else {
      avg = (avg + 50) / 100;
      best = calcBest(cents, avg, numPpl);
    }
    cout << "$" << (best / 100) << "." << (best % 100) / 10 << (best % 10) << endl;
  }
  return 0;
}
Here are my tests:

3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
3
6.17
5.00
4.03
12
123.12
6.13
9.44
89.08
278.78
223.78
78.45
912.89
554.76
547.57
1781.89
907.07
2
4.99
15.00
1
10.00
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
5
5000.00
11.11
11.11
11.11
11.11
15
0.01
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
4
25.00
25.00
25.00
28.00
3
10.01
15.25
18.96
4
25.03
25.00
25.00
25.00
0

I expect that to return:

$10.00
$11.99
$1.10
$2407.09
$5.00
$0.00
$0.01
$3991.11
$0.01
$2.25
$4.73
$0.02

And it does. Any help?