Advertisement
Guest User

Solution Code for the Clock Problem

a guest
Oct 15th, 2019
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>  // Includes all standard libraries - useful for contests.
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     string input;
  8.     double hour, minute;
  9.     char colon;
  10.  
  11.     // Sets the precision.
  12.     cout << fixed << setprecision(3);
  13.  
  14.     while (true)
  15.     {
  16.         // Handle the input.
  17.         cin >> hour >> colon >> minute;
  18.  
  19.         // 0:00 should trigger a program termination.
  20.         if (hour == 0 && minute == 0)
  21.         {
  22.             return 0;
  23.         }
  24.  
  25.         if (hour == 12)
  26.         {
  27.             hour = 0;
  28.         }
  29.  
  30.         // Convert the hours and minutes into angles.
  31.         hour *= 30;
  32.         hour += ((minute / 60) * 30);
  33.         minute *= 6;
  34.  
  35.         // We can't just get the absolute difference as one hand might be at
  36.         // 350 degrees while the other is at 10 degrees. The expected result would
  37.         // be 20 degrees, but abs(350 - 10) is 340.
  38.         // To fix this, we need to measure the reflex angle.
  39.         // We can do this by checking if subtracting abs(350 - 10) from 360 makes
  40.         // it smaller.
  41.         cout << min(abs(hour - minute), 360.0 - abs(hour - minute)) << endl;
  42.     }
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement