/* Problem 3--Those Wacky Clocks
   A reasonably easy way to do this problem is to first find out when
   they coincide, and then figure out what that time is.  The easiest
   way to the first part is to compute the relative rate of the faster
   clock, like computing the relative rate of a faster car passing a
   slower car. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MIN(a,b) ((a)<(b)?(a):(b))

int main (int argc, char **argv);
void Process (int h1, int m1, int r1, int h2, int m2, int r2);
double Intersect (int h1, int m1, int h2, int m2, int s1);

FILE *in, *out;

int main (int argc, char **argv) {

  int h1, m1, r1, h2, m2, r2;

 in = fopen ("prob3.in","r");
 out = fopen ("prob3.out","w");
 while (1) {
  fscanf (in,"%d",&h1);
  if (h1==0) break;  /* read in the hours, minutes, and rates */
  fscanf (in,"%d %d %d %d %d",&m1,&r1,&h2,&m2,&r2);
  Process (h1,m1,r1,h2,m2,r2);
 }
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}

/* This computes the converging point, if one exists */

void Process (int h1, int m1, int r1, int h2, int m2, int r2) {

  int s1, s2;
  double t, fm1;

 if (h1==h2 && m1==m2) {  /* If the clocks already coincide */
  fprintf (out,"The clocks will converge at %d:%02d.\n",h1,m1);
  return;
 }
 if (r1 == r2) { /*If the clocks are different, but rates are the same*/
  fprintf (out, "The clocks will never converge.\n");
  return;
 }
 s1 = r1 - MIN(r1,r2); /* The difference in rates remains the same, */
 s2 = r2 - MIN(r1,r2); /* but the slowest clock is halted. */
 t = s2==0 ? Intersect (h1,m1,h2,m2,s1) : Intersect (h2,m2,h1,m1,s2);
   /* How many minutes will elapse for the moving clock to reach the
      time displayed on the halted clock, or how minutes will it take
      for the two original clocks to coincide */
 fm1 = m1 + r1/100.0 * t;  /* Checking what the first clock will read*/
 if (fabs (fm1-(int)(.5+fm1)) < 1e-5) /* avoiding round-off error */
  m1 = (int) (.5+fm1);
 else
  m1 = (int) fm1;  /* rounding down */
 h1 += m1/60; /* Normalizing the time so that hours and minutes are */
 m1 %= 60;    /* in their correct ranges. */
 h1 = (h1-1)%12+1;
 fprintf (out,"The clocks will converge at %d:%02d.\n",h1,m1);
}

/* Intersect computes how long it will take one clock moving at a
   specific rate will take to read a certain time */

double Intersect (int h1, int m1, int h2, int m2, int s1) {

  int md = (h2*60+m2)-(h1*60+m1);  /* The difference in minutes */

 if (md < 0) md += 12*60;  /* Normalize if negative */
 return (md*100.0/s1); /* Factor in the rate */
}

