10281 - Average Speed
Moderator: Board moderators
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]
[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]
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;
}
-
- New poster
- Posts: 36
- Joined: Mon Jun 19, 2006 5:43 pm
- Location: Bangladesh
- Contact:
10281 wa, plz help!
Last edited by deena sultana on Thu Jun 22, 2006 8:01 pm, edited 1 time in total.
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
Use instead of as specifier incase of double
Code: Select all
%lf
Code: Select all
%f
-
- New poster
- Posts: 36
- Joined: Mon Jun 19, 2006 5:43 pm
- Location: Bangladesh
- Contact:
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
another mistake (Judge will give you WA in this case)
You wrote:
Correction:
keep posting
You wrote:
Code: Select all
emotional_blind
Code: Select all
emotional blind
-
- New poster
- Posts: 36
- Joined: Mon Jun 19, 2006 5:43 pm
- Location: Bangladesh
- Contact:
10281 -WA HELP PLEASE
Hi, friends.
i got wa for 10281.
[/quote]
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;
}
-
- Learning poster
- Posts: 56
- Joined: Tue Jun 13, 2006 5:18 pm
- Location: (CSE, SUST) Sylhet, Bangladesh
- Contact:
-
- 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 .