Page 9 of 11
Where is the problem(10137 - The Trip)
Posted: Sat Sep 14, 2013 6:44 pm
by shikhorroy
Help me please....I get WA....
Code: Select all
#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 1001
#define int64 long long
int main()
{
int n, cost[MAX];
while(scanf("%d",&n))
{
if(n == 0) break;
int val, fVal;
int64 total = 0, i;
for(i = 1; i <= n; i++)
{
scanf("%d.%d",&val,&fVal);
cost[i] = (val * 100) + fVal;
total += cost[i];
}
double average = total / (100.0 * n), result = 0.0;
average = (int64)(average * 100) / 100.0;
for(i = 1; i <= n; i++)
{
float x = (cost[i] / 100.0);
if(x < average) result += (average - x);
}
printf("$%.2lf\n",result);
}
return 0;
}
Re: Where is the problem(10137 - The Trip)
Posted: Mon Sep 16, 2013 9:58 am
by shikhorroy
I got AC..
Re: 10137 - The Trip
Posted: Thu Nov 07, 2013 9:57 pm
by TheGodHasCome
now it's 2013 and still wrong answer makes people mad with this one. So, i did it in pascal, it looks quite straightforward. I tries all tests mentioned within these 8 pages and got right answers. But when i submit it, i get WA. Can anyone suggest what may be wrong? Code:
program trip(input, output);
var a:array[1..1000] of real;
i,j,n:integer;
avg,sum,exc1,exc2,lowAvg,highAvg:real;
begin
repeat
exc1:=0;exc2:=0; avg:=0;sum:=0;
readln(n);
if n=0 then break;
for j:=1 to n do readln(a[j]);
for j:=1 to n do sum:=sum+a[j];
avg:=sum/n;
if avg=trunc(avg) then begin
lowAvg:=avg;
highAvg:=avg;
end;
if avg<>trunc(avg) then begin
avg:=trunc(avg*100)/100;
lowAvg:=avg;
highAvg:=avg+0.01;
end;
for j:=1 to n do if a[j]<lowAvg then exc1:=exc1+(lowAvg-a[j]);
for j:=1 to n do if a[j]>highAvg then exc2:=exc2+(a[j]-highAvg);
if exc1>exc2 then writeln(exc1:8:2)
else writeln(exc2:8:2);
until EOF(input);
end.
any ideas? thank you
Re: 10137 - The Trip
Posted: Fri Nov 08, 2013 10:37 pm
by brianfry713
brianfry713 wrote:Try solving it without using floating point.
Re: 10137 - The Trip
Posted: Wed Nov 13, 2013 12:54 pm
by venkikumar.m
I tried with all inputs , but it shows WA, I am frustrated with this prob....anybody help me....
here my code....
Code: Select all
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[100];
long long n,len,i;
long long x,sum,med,s;
cin>>n;
while(n)
{
sum=0LL;
long long *a=new long long[n];
for(i=0LL;i<n;i++)
{
cin>>str;
x=0LL;
len=strlen(str);
for(int j=0LL;j<len;j++)
{
if(str[j]!='.')
x=x*10LL+(str[j]-'0');
}
a[i]=x;
sum=sum+a[i];
}
// cout<<sum;
s=sum*100LL/(n);
if((s%100LL)>=45LL)
{
s=s/100LL;
s=s+1LL;
}
else
{
s=s/100LL;
}
// cout<<s<<endl;
med=s;
sum=0LL;
s=0LL;
for(i=0LL;i<n;i++)
{
if(a[i]<med)
sum=sum+(med-a[i]);
if(a[i]>med)
s=s+(a[i]-med);
}
if(s<sum)
sum=s;
cout<<"$"<<sum/100LL<<".";
if(sum%100LL==0LL)
cout<<"00\n";
else if(sum%100LL<=9LL)
cout<<"0"<<sum%10LL<<endl;
else
cout<<sum%100LL<<endl;
delete a;
cin>>n;
}
return 0;
}
Re: 10137 - The Trip
Posted: Thu Nov 14, 2013 12:32 am
by brianfry713
Input:
Code: Select all
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
AC output:
Re: 10137 - The Trip
Posted: Thu Nov 14, 2013 6:30 pm
by venkikumar.m
I am getting
as output....
My approach is calculate the average of all and
calculated sum as
Code: Select all
sum+=(a[i]-avg) if a[i]>avg
sum1+=(avg-a[i]) if a[i]<avg
if(sum>sum1)
sum=sum1;
prints the sum value....
this is my approach ....
is this wrong ..... ?
Re: 10137 - The Trip
Posted: Thu Nov 14, 2013 10:56 pm
by brianfry713
Yes your code is wrong on the I/O I posted. If the person who spent 0.03 takes 0.01 from one of the people who spent 0.01 then they are all equal within a cent.
Re: 10137 - The Trip
Posted: Mon Nov 18, 2013 6:14 am
by venkikumar.m
I changed my code still shows WA....
Code: Select all
#include<iostream>
#include<numeric>
using namespace std;
double calculate(double *amount,int n)
{
int i;
double avg,high=0.0,low=0.0;
for(i=0;i<n;i++)
avg=amount[i]+avg;
avg=avg/n;
for(i=0;i<n;i++)
{
if(amount[i]>=avg)
{
high+=(double)(long)((amount[i]-avg)*100.0)/100.0;
}
else
{
low+=(double)(long)((avg-amount[i])*100.0)/100.0;
}
}
if(high>low)
return (double)high;
return (double)low;
}
int main()
{
int n;
cin>>n;
while(n)
{
double *amount=new double[n];
double exchange;
for(int i=0;i<n;i++)
cin>>amount[i];
exchange=calculate(amount,n);
cout.precision(2);
cout.setf(ios::fixed,ios::floatfield);
cout<<"$"<<exchange<<endl;
delete amount;
cin>>n;
}
}
Re: 10137 - The Trip
Posted: Mon Nov 18, 2013 10:58 pm
by brianfry713
brianfry713 wrote:brianfry713 wrote:Try solving it without using floating point.
uva 10137 the trip getting WA
Posted: Thu Jan 30, 2014 8:22 pm
by unreleased
i cant find error whats wrong in my code pls helpan
#include<stdio.h>
int main()
{
int a, b, c, d, st_num,rest;
double amnt[1010], avrg, ans, total;
while(scanf("%d", &st_num)&&st_num!=0)
{
total=0;
for(a=0; a<st_num; a++)
{
scanf("%lf", &amnt[a]);
total+=amnt[a];
}
avrg=total/st_num;
ans=0;
for(b=0; b<st_num; b++)
{
if(amnt>avrg)
{
rest=(amnt-avrg)*100;
ans+=rest;
}
}
printf("$%.2lf\n", ans/100);
}
return 0;
}
Re: uva 10137 the trip getting WA
Posted: Thu Jan 30, 2014 10:17 pm
by brianfry713
Try solving it without using floating point.
Re: uva 10137 the trip getting WA
Posted: Mon Feb 03, 2014 1:25 pm
by unreleased
would u please elaborate.........
i didnt get it clearly.......... :p
Re: uva 10137 the trip getting WA
Posted: Mon Feb 03, 2014 9:53 pm
by brianfry713
Use only integers instead of double.
10137 - The Trip, get WA
Posted: Mon Feb 17, 2014 9:34 am
by gg004
Here is my code.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void format(char s[]) {
int i = 0;
while(isdigit(s[i])) {
i++;
}
if (s[i] == '.') {
while (s[i]) {
s[i] = s[i + 1];
i++;
}
}
else {
s[i++] = '0';
s[i++] = '0';
s[i] = '\0';
}
}
int main(void) {
int i, n;
int sum, min, max, take, give;
int unit[1000];
char buff[20];
while (gets(buff) != NULL) {
sscanf(buff, "%d", &n);
if (n) {
sum = 0;
for (i = 0; i < n; ++i) {
gets(buff);
format(buff);
unit[i] = atoi(buff);
sum += unit[i];
}
min = sum / n;
max = (min * n != sum) ? min + 1 : min;
take = give = 0;
for (i = 0; i < n; ++i) {
if (unit[i] < min) {
give = give + (min - unit[i]);
}
else if (unit[i] > max) {
take = take + (unit[i] - max);
}
}
printf("$%.2f\n", ((give > take) ? give : take) / 100.0f);
} else {
break;
}
}
return EXIT_SUCCESS;
}
I passed all the test case that I can find in the board, but still got WA.
Someone help me