## 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

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

### 10281 - Average Speed

Why does this code get WA?
[c]
#include<stdio.h>
#include<string.h>
void main(void)
{
int count,x;
long y,nowh,nowm,nows,h,m,s;
double dis,speed,t;
char time[10],buffer[10];
for(count=1,dis=0,speed=0;scanf("%s",time)!=EOF;count++)
{
gets(buffer);
if(!strlen(buffer) && count==1)
{
printf("%s 00.00km\n",time);
break;
}
nowh=(time[0]-'0')*10+time[1]-'0';
nowm=(time[3]-'0')*10+time[4]-'0';
nows=(time[6]-'0')*10+time[7]-'0';
if(count!=1)
{
y=(nowh-h)*3600+(nowm-m)*60+(nows-s);
dis+=(speed*((double)y/3600));
}
if(!strlen(buffer))
printf("%s %.2lfkm\n",time,dis);
else
{
for(x=0,t=0;buffer[x]!='\0';x++)
if(buffer[x]>='0' && buffer[x]<='9')
t=t*10+buffer[x]-'0';
speed=t;
}
h=nowh,m=nowm,s=nows;
}
}
[/c]

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:
why do you 'break' when (!strlen(buffer) && count==1) ?
i think 00.00km should be 0.00km

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan
I've fix it and got accepted, thanks.

Sederik
New poster
Posts: 3
Joined: Sat Dec 10, 2005 1:56 pm
Hi. Please, help me find a reason for getting constant Wrong Answer!

Code: Select all

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

int main ()
{
char line[6], tmp[2];
int ho, mo, sec, speed = 0, i;
float distance = 0, time = 0;
while (scanf("%d:%d:%[0-9 ]", &ho, &mo, line)!=EOF)
{
strncpy(tmp, line, 2);
sec = atoi(tmp);
distance+=(ho + mo/60.0 + sec/3600.0 - time)*speed;
time = ho + mo/60.0 + sec/3600.0;

if (strlen(line)>2)
{
for (i = 0; i<4; i++)
line[i] = line[i+3];
speed = atoi(line);
}
else
printf("%02d:%02d:%02d %.02f km\n", ho, mo, sec, distance);
}
return 0;
}``````

Sederik
New poster
Posts: 3
Joined: Sat Dec 10, 2005 1:56 pm
Sorry, already fixed it.

deena sultana
New poster
Posts: 36
Joined: Mon Jun 19, 2006 5:43 pm
Contact:

### 10281 wa, plz help!

Hi, friends.
i got wa for 10281.

is anyone there 2 help me? plzzzzzzzzz.

Code: Select all

``````
``````
Last edited by deena sultana on Thu Jun 22, 2006 8:01 pm, edited 1 time in total.

emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Contact:
Use

Code: Select all

``%lf``

Code: Select all

``%f``
as specifier incase of double

deena sultana
New poster
Posts: 36
Joined: Mon Jun 19, 2006 5:43 pm
Contact:
dear emotional_blind
THANK U SO MUCH. Its ac now!

emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Contact:
another mistake (Judge will give you WA in this case)

You wrote:

Code: Select all

``emotional_blind``
Correction:

Code: Select all

``emotional blind``
keep posting

deena sultana
New poster
Posts: 36
Joined: Mon Jun 19, 2006 5:43 pm
Contact:
Ohhho , that's why i get so much wa.right?

emotional blind, i m sorry.
u r great! thnx.

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia

### 10281 -WA HELP PLEASE

Hi, friends.
i got wa for 10281.

Code: Select all

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

#ifndef ONLINE_JUDGE
#include <io.h>
#include <fcntl.h>
#endif

int main()
{
#ifndef ONLINE_JUDGE
close(0);
open("in.txt",O_RDONLY);
#endif
int h,m,s,col=0;
char str[80],time[10];
long long time1,time2;
double speed,dist=0,t;
while (scanf("%s",time)!=EOF)
{
sscanf(time,"%i%*c%i%*c%i",&h,&m,&s);
str[0]='\0';
gets(str);
time2=(s+60*(((long long)h)*60+m));
if (sscanf(str,"%lf",&t)!=EOF)
{
if (col) dist+=(time2-time1)*speed/3600.0;
col++;
time1=time2;
speed=t;
}
else
{
if (col) printf("%s %.2lf km\n",time,(double)(dist+(time2-time1)*speed/3600.0));
else printf("%s 0.00 km\n",time);
}
}
return 0;
}``````
[/quote]

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
%i treats numbers with leading zeroes as octal numbers, I think. Use %d instead.

Zaspire
New poster
Posts: 36
Joined: Sun Apr 23, 2006 2:42 pm
Location: Russia
Thanks you.
I got AC now

Mushfiqur Rahman
Learning poster
Posts: 56
Joined: Tue Jun 13, 2006 5:18 pm
Location: (CSE, SUST) Sylhet, Bangladesh
Contact:
Sederik wrote
Sorry, already fixed it.
Then u should remove your code from the thread. Please do it.

Mushfiqur Rahman
Learning poster
Posts: 56
Joined: Tue Jun 13, 2006 5:18 pm
Location: (CSE, SUST) Sylhet, Bangladesh
Contact:

Code: Select all

``````Now I got Accepted. The only special case of this problem is that there may trailling spaces and speed can be 0 km/hour .
``````