10137 - The Trip
Moderator: Board moderators
Got it here, too
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.
Thanks.
10137 help plz
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;
}
}
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;
}
}
10137 HELPPPPPPPPPPPPP!!!!
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);
}
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.
![:-?](./images/smilies/icon_confused.gif)
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
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
[/cpp]
![:lol:](./images/smilies/icon_lol.gif)
![:lol:](./images/smilies/icon_lol.gif)
![:lol:](./images/smilies/icon_lol.gif)
![:lol:](./images/smilies/icon_lol.gif)
10137 Why WA ?
[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]
#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]
10137 - The Trip
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]
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]
My own quote:
We are here as Adam and Eve were here!
We are here as Adam and Eve were here!
The problem lies in the fact that you can be one penny off. So where you do
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.
Code: Select all
ans += trip[i] - avg;
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:
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?
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;
}
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?