/* Problem 4--Great Circle Trajectory This was intended to be the hardest problem in the set, requiring a thorough knowledge of trigonometry. Note that the ACM contest almost always have a trigonometric problem. If you are dismayed that there is a "magic formula" that solves the problem that you may not have known, well, I'm as surprised as you. I didn't know there was a magic formula until I worked one out while writing this solution program. */ #include #include #include #define PI 3.14159265358979323846 FILE *in, *out; double slat, slong, dlat, dlong; char slatdir, slongdir, dlatdir, dlongdir; int main (int argc, char **argv); void Process (void); int main (int argc, char **argv) { in = fopen ("prob4.in","r"); out = fopen ("prob4.out","w"); /* read in data */ while (fscanf (in,"%lf",&slat),slat >= 0) { fscanf (in,"%1s %lf %1s %lf %1s %lf %1s",&slatdir,&slong,&slongdir, &dlat,&dlatdir,&dlong,&dlongdir); Process (); } fclose (in); fclose (out); return EXIT_SUCCESS; } /* Process processes a test case. */ void Process (void) { double traj; if (slatdir=='S') slat = -slat; /* Negate angle if necessary */ if (dlatdir=='S') dlat = -dlat; if (slongdir=='W') slong = -slong; if (dlongdir=='W') dlong = -dlong; slat *= PI/180; slong *= PI/180; /* Convert to radians */ dlat *= PI/180; dlong *= PI/180; /* Magic formula */ traj = atan2 (cos(dlat)*sin(dlong-slong), cos(slat)*sin(dlat)-sin(slat)*cos(dlat)*cos(dlong-slong)); traj *= 180/PI; /* convert to degrees */ if (fabs (traj) < 0.005) traj = 0; if (traj < 0) fprintf (out,"%.2f degrees west of north\n",-traj); else fprintf (out,"%.2f degrees east of north\n",traj); }