10339 - Watching Watches

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

Moderator: Board moderators

Post Reply
User avatar
Christian Schuster
Learning poster
Posts: 63
Joined: Thu Apr 04, 2002 2:00 am

10339 - Watching Watches

Post by Christian Schuster » 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?

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard » Tue Jul 30, 2002 11:23 am

kd = d * (86400.0-k)/86400.0

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » 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.

User avatar
Christian Schuster
Learning poster
Posts: 63
Joined: Thu Apr 04, 2002 2:00 am

Post by Christian Schuster » Tue Jul 30, 2002 12:23 pm

Thank you, got AC.

The wrong formula was a typing mistake in my post. :oops:

The main problem really was the rounding error! Stupid mistake.

Hauser
New poster
Posts: 4
Joined: Tue Aug 13, 2002 7:32 pm

Post by Hauser » 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

stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

more test cases

Post by stcheung » 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.

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

10339 - Why WA ???

Post by Almost Human » 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 ?

thanks in advance

Andrew Neitsch
New poster
Posts: 43
Joined: Fri Jun 25, 2004 9:37 pm

Post by Andrew Neitsch » 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

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » 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.
Ami ekhono shopno dekhi...
HomePage

ibroker
New poster
Posts: 18
Joined: Tue Nov 08, 2005 6:38 pm

10339 Watcing Watches

Post by ibroker » 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

dibery
Learning poster
Posts: 76
Joined: Sat Feb 23, 2013 4:16 pm
Location: Taiwan, Taipei
Contact:

Re: 10339 - Watching watches

Post by dibery » 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.
Life shouldn't be null.

Post Reply

Return to “Volume 103 (10300-10399)”