Page 1 of 1

### 10339 - Watching Watches

Posted: Tue Jul 30, 2002 9:40 am
Hello!

Problem 10339 seems tricky to me. My idea to solve it was:

When a (real) day has passed, the k-watch has counted (86400-k) seconds. The same holds for the m-watch and (86400-m) seconds.

I calculate the number of (real) days, when the time difference between the two watches becomes 12 hours (43200 seconds) with

d = 43200.0 / fabs(k-m)

After "d" real days have passed, the k-watch has run

kd = d * 86400.0/(86400.0-k)

"k-days". The value

mins = (int)floor(1440.0*fmod(kd,0.5)+0.5)

should then give the minutes since midnight (or noon), when the watches meet. To determine the hour and minute to "mins" should be no problem.

I get WA - what did I do wrong?

Posted: Tue Jul 30, 2002 11:23 am
kd = d * (86400.0-k)/86400.0

Posted: Tue Jul 30, 2002 12:05 pm
Use mins = floor(1440.0*fmod(kd+1e-7,0.5)+0.5);
to avoid floating point errors.

Posted: Tue Jul 30, 2002 12:23 pm
Thank you, got AC.

The wrong formula was a typing mistake in my post. The main problem really was the rounding error! Stupid mistake.

Posted: Sun Aug 18, 2002 3:27 pm
It can be done also without floats.

Let x be the difference between the two watches after one day (in seconds)

x = |a-b|

So the difference between two watches after one second (y) will be

y = x / 86400

Both watches will show the same time after the difference between
their times will be 12h, that is 43200 seconds, so

t = 43200 / y

Where t is the real time after which watches will be synchronized.
First watch goes (86400-a)/86400 times slower than "real time clocks".
So it will show:

seconds = t * (86400-a)/86400 = ( (86400-a)*43200 ) / |a-b|
minutes = ( (86400-a)*43200 ) / ( 60*|a-b| )

'minutes' looks like that m/n, what we have to do is to divide
'm' by 'n' treating them as integers (result will be floor-ed ),
and then check out if we have to add one to round result good

if( (m%n)*2 >= n )
minutes++;

And we have our result. It's simply, isn't it? (Use long longs, otherwise integer overflow guaranteed.)

Hauser

### more test cases

Posted: Wed May 21, 2003 3:25 am
Hmm...I keep getting WA, and I basically used the method outlined in the first post. Can anyone provide me more test cases to check my program? Thanks in advance.

### 10339 - Why WA ???

Posted: Sat May 24, 2003 10:57 am

Code: Select all

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

int main ( void )
{
long double result ;
int late1 , late2 , input1 , input2 ;
unsigned long day ;
int hour , minute ;

/*	freopen ( "10339.in" , "r" , stdin ) ;
freopen ( "10339.out" , "w" , stdout ) ;*/

while ( scanf ( "%i %i" , &late1 , &late2 ) != EOF )
{
input1 = late1 ; input2 = late2 ;

if ( input1 < input2 ) input1 ^= input2 ^= input1 ^= input2 ;

if ( input1 - input2 )
result = 43200.0 / ( long double ) ( input1 - input2 ) ;
else
result = ( long double ) 1 / 1440 ;

result = result * ( ( long double ) ( 86400 - input2 ) / 86400.0 ) ;

day = ( unsigned long ) result ;
result = result - day ;

result = ( result * 24 * 60 ) ;
if ( result - ( unsigned long ) result >= 0.5 ) result = ( unsigned long ) result + 1 ;
else result = ( unsigned long ) result ;

hour = result / 60 ;
minute = result - ( hour * 60 ) ;
if ( hour == 0 ) hour = 12 ;

printf ( "%i %i %02i:%02i\n" , late1 , late2 , hour , minute ) ;
}

return 0 ;
}
``````
Is there any special input to be considered ?

Posted: Mon Jul 19, 2004 10:44 pm
Adding +EPS to my fmod() call changed only the following output, which was then AC:

2 194 11:53

Posted: Sun Sep 11, 2005 12:49 am
When k and m are equal you should print 'k m 12:00'.

Your code returns 12:01 when k and m are equal.

### 10339 Watcing Watches

Posted: Mon Jul 03, 2006 4:25 am
I think my solution correct, but I got WA.

d = 43200.0/fabs((double)(a-b));
d = d*(86400-a)/(86400.);
hour = (d - (int)d)*24.;
minute = (hour - (int)hour) * 60;
hour = (int)hour;

a and b are input integer.

d is the real day which watches will be synchronized.

watch goes (86400-a)/(86400) days slower than real day.

plz help me

### Re: 10339 - Watching watches

Posted: Thu Aug 07, 2014 6:51 am
Here are some cases useful when I'm debugging.

0 11 06:33
0 17 04:14
1 184 01:30

Notice that:
1. We should round to the "nearest" minute. If the time is 4:39:30, then the rounded result should be 4:40, not 4:39.
2. Don't round seconds to integral value first. If the time is 1:23:29.52, don't round to 1:23:30 first. It should be 1:23.