/******* 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 using namespace std; int main (int argc, char **argv); double angle (int h, int m); ifstream in ("prob4.in"); ofstream out ("prob4.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 while (1) { // read in hour-minute pairs and print out in >> h >> m; // the angle between them until hour and if (h==0 && m==0) break; // minute are both 0. out << "At " << h << ":"; out.width(2); out.fill ('0'); out << m << " the angle is "; int a = (int) (10*angle (h,m)+0.5); out.width(0); out << a/10 << "." << a%10 << " degrees." << endl; } 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. **********************************************************************/ 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 }