/******* Problem 4--Clock Angles ************************************** One important fact about these programming contests is that the problems are not ordered by difficulty. Before the contest, they announced that one of the problems was essentially a "Hello World" problem, meaning that it was easy. This was that problem. Most teams were able to get this one. The computation was straightforward. **********************************************************************/ #include #include #include int main (int argc, char **argv); static double angle (int h, int m); FILE *in, *out; /******* main ********************************************************* The program reads a series of hour-minute pairs from the input file and prints the angle between the clock hands correct to the nearest tenth of a degree. *********************************************************************/ int main (int argc, char **argv) { int h, m; /* The hour-minute pair */ in = fopen ("prob4.in","r"); /* redirecting input and output */ out = fopen ("prob4.out","w"); while (1) { /* read in hour-minute pairs and print out */ fscanf (in,"%d %d",&h,&m); /* the angle between them until hour and */ if (h==0 && m==0) break; /* minute are both 0. */ fprintf (out,"At %d:%02d the angle is %.1f degrees.\n",h,m, angle(h,m)); } fclose (in); /* closing the files */ fclose (out); return EXIT_SUCCESS; } /******** angle ******************************************************* angle accepts two integers, the hour and minute, and returns a double containing the angle between the two hands of the clock. The angle must be between 0 and 180 degrees. **********************************************************************/ static double angle (int h, int m) { double ha, ma, /* The angle of the hour and minute hands */ a; /* The angle (to be returned) between the hands */ ma = m * 6; /* The difference between minutes is 6 degrees each */ ha = (h%12) * 30 + ma/12; /* The difference between hours is 30 degrees each, plus each time the minute hand is advanced, the hour hand is advanced by one-twelfth as much. */ a = fabs (ma-ha); /* The distance between the hands in degrees */ return a > 180 ? 360-a : a; /* Making sure we return the smaller angle between the hands */ }