10339 - Watching Watches
Moderator: Board moderators
-
- Learning poster
- Posts: 63
- Joined: Thu Apr 04, 2002 2:00 am
10339 - Watching Watches
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?
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?
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
-
- Learning poster
- Posts: 63
- Joined: Thu Apr 04, 2002 2:00 am
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?![;)](./images/smilies/icon_wink.gif)
(Use long longs, otherwise integer overflow guaranteed.)
Hauser
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?
![;)](./images/smilies/icon_wink.gif)
(Use long longs, otherwise integer overflow guaranteed.)
Hauser
more test cases
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.
-
- Learning poster
- Posts: 93
- Joined: Sun Jan 12, 2003 3:30 pm
10339 - Why WA ???
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 ;
}
thanks in advance
-
- New poster
- Posts: 43
- Joined: Fri Jun 25, 2004 9:37 pm
When k and m are equal you should print 'k m 12:00'.
Your code returns 12:01 when k and m are equal.
Your code returns 12:01 when k and m are equal.
Ami ekhono shopno dekhi...
HomePage
HomePage
10339 Watcing Watches
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
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
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.
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.