10281 - Average Speed

All about problems in Volume 102. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

nahid
New poster
Posts: 18
Joined: Wed Oct 04, 2006 8:59 pm
Location: DHAKA,BANGLADESH
Contact:

m2 WA

i'm getting WA on this code. could u tell pls, why?

Code: Select all

``````#include<stdio.h>

int main()
{
double spd,pspd,pdis=0,tdis=0,itime=0,ctime;
long int hour;
int min,sec;
char c1,c2,c3;

while(scanf("%ld%c%d%c%d%c",&hour,&c1,&min,&c2,&sec,&c3)!=EOF)
{
if(c3==' ')
{
scanf("%lf",&spd);
ctime = (double)hour + (double)min/60 + (double)sec/3600;
if(tdis!=0)
{
pdis = (double)pspd * (double)(ctime - itime);
}

itime = ctime;
pspd = spd;
}

else
{
ctime = (double)hour + (double)min/60 + (double)sec/3600;

tdis = (double)spd * (double)(ctime - itime);

if(hour<10)
printf("0");
printf("%ld%c",hour,c1);

if(min<10)
printf("0");
printf("%d%c",min,c2);

if(sec<10)
printf("0");
printf("%d %.2lf km\n",sec,(tdis+pdis));
}
}
return 0;
}``````

ishtiaq ahmed
Learning poster
Posts: 53
Joined: Sat Jul 29, 2006 7:33 am
Location: (CSE,DU), Dhaka,Bangladesh

10281 - Problem A: Average Speed

This code hangs for WA. Anybody help me?

Code: Select all

``````/*
Problem No: 10281
Problem : Average Speed
Algorithm: Adhoc
Author : Ishtiaq Ahmed
*/

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>

char str[1000];
double temp, tempo, pre_hour,  total_distance = 0, pre_min, pre_sec, hour, min, sec, distance, total_time, avg_speed;

int main()
{
//freopen("c:\\input.txt", "r", stdin);
//freopen("c:\\output.txt", "w", stdout);
double count;
gets(str);
sscanf(str, "%lf:%lf:%lf %lf", &pre_hour, &pre_min, &pre_sec, &distance);
//scanf("%*c");

total_time = 0;
avg_speed = distance / 60 / 60;
int print_flag;
int flag;
while(gets(str))
{
total_time = 0;
print_flag = 1;
count = -1;
sscanf(str, "%lf:%lf:%lf %lf", &hour, &min, &sec, &count);
if(fabs(count +1) > 0.000001)
//		if(count != -1)
{
print_flag = 0;
distance =count;
}
flag = 0;
if(pre_sec > sec)
{

total_time  += (sec + 60 - pre_sec);
flag = 1;
}

else
total_time += (sec - pre_sec);
temp = pre_min;

if(flag == 1)
temp++;
flag = 0;
if(temp > min)
{
flag = 1;
total_time += (min + 60 - temp) * 60;
}
else
total_time += (min - temp) * 60;
temp = pre_hour;
if(flag == 1)
temp++;
flag = 0;
total_time += (hour - temp) * 60 * 60;

tempo = total_time * avg_speed;
total_distance += tempo;
if(print_flag == 1)
{
if(hour < 10)
printf("0");
printf("%.0lf:", hour);
if(min < 10)
printf("0");
printf("%.0lf:",min);
if(sec < 10)
printf("0");
printf("%.0lf ",sec);

printf("%.2lf km\n", total_distance + .0000001);

}

pre_hour = hour;
pre_min = min;
pre_sec = sec;
avg_speed = distance / 60 / 60;

}

return 0;
}

``````
No venture no gain

with best regards
------------------------
ishtiaq ahmed

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

10281 - Average Speed

can someone post me some some good i/o???
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

m_helal
New poster
Posts: 1
Joined: Tue Apr 05, 2011 11:24 am

10281 - Average Speed

What is the problem in that code ? plz help me

#include<stdio.h>
#include<string.h>
int main()
{
long sec,speed,i,j,temp;
long double distance;
sec=0,speed=0;
distance=0;
char str[100];
while(gets(str))
{
int len = strlen(str);
if(len>8)
{
temp = ((((str[0]-'0')*10)+(str[1]-'0'))*60*60)+
((((str[3]-'0')*10)+(str[4]-'0'))*60)+
(((str[6]-'0')*10)+(str[7]-'0'));
distance+=((temp-sec)*speed)/3600;
sec = temp;
speed = 0;
for(i=len-1,j=1;i>8;i--,j*=10)
{
speed+=(str-'0')*j;
}
}
else
{
temp = ((((str[0]-'0')*10)+(str[1]-'0'))*60*60)+
((((str[3]-'0')*10)+(str[4]-'0'))*60)+
(((str[6]-'0')*10)+(str[7]-'0'));
distance+=((temp-sec)*speed)/3600.0;

sec = temp;

for(i=0;i<8;i++)
{
printf("%c",str);
}
printf(" %.2Lf km\n",distance);
}
}
return 0;
}

live_lie
New poster
Posts: 19
Joined: Mon Nov 29, 2010 11:50 pm

Re: 10281 - Average Speed

what will be the output for this input
00:00:01 100
01:00:01 50
00:00:01

please someone answer

sijan
New poster
Posts: 5
Joined: Fri Sep 09, 2011 6:44 pm

10281 - Average Speed WA

I am getting WA...Plzz help me...

Code: Select all

``````Delete After AC...
``````
Last edited by sijan on Fri Feb 01, 2013 9:50 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10281 - Average Speed WA

Try rewriting it so it will pass with extra spaces and missing leading zeros in the input.
Check input and AC output for thousands of problems on uDebug!

tzupengwang
New poster
Posts: 36
Joined: Fri Dec 02, 2011 1:30 pm
Location: Kaohsiung, Taiwan

Re: 10281 - Average Speed

Could anyone help with my WA?
I can't figure out the tricky part in the question~

Code: Select all

``````/*10281*/
Removed after AC
``````
Last edited by tzupengwang on Wed Aug 15, 2012 7:33 pm, edited 5 times in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10281 - Average Speed

now should be a double.
Check input and AC output for thousands of problems on uDebug!

tzupengwang
New poster
Posts: 36
Joined: Fri Dec 02, 2011 1:30 pm
Location: Kaohsiung, Taiwan

Re: 10281 - Average Speed

Thanks for your help~
I've changed "now" to double as the above but still receive WA now
Are there further problems with it?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10281 - Average Speed

Input:

Code: Select all

``````00:00:00 2000000000
99:59:59``````
AC output:

Code: Select all

``99:59:59 199999444444.44 km``
Check input and AC output for thousands of problems on uDebug!

tzupengwang
New poster
Posts: 36
Joined: Fri Dec 02, 2011 1:30 pm
Location: Kaohsiung, Taiwan

Re: 10281 - Average Speed

OH~
I see, so this one requires BIG NUM CALCULATION~

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10281 - Average Speed

No, my AC C++ code uses only int and double for calculations. Your code does overflow where mine doesn't.
Check input and AC output for thousands of problems on uDebug!

tzupengwang
New poster
Posts: 36
Joined: Fri Dec 02, 2011 1:30 pm
Location: Kaohsiung, Taiwan

Re: 10281 - Average Speed

Thanks for your help
I alter my codes again, which do not overflow and gets right with your I/O
However, I am still receiving WA!
What other problem is happening?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10281 - Average Speed

It could be a precision error now. Try updating your now variable at each query.
Check input and AC output for thousands of problems on uDebug!