How many times a day does a clock’s hands overlap?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Runtime.InteropServices;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //How many times a day does a clock’s hands overlap
            //Printing the 12 hours time using two for loops
            int count = 0;
            Queue angleQueue = new Queue();
            Queue timeQueue = new Queue();
            for (int i = 0; i <= 23; i++)
            {
                for (int j = 0; j <= 59; j++)
                {
                    if (getAngleInDegrees(i, j) == 0)
                    {
                        Console.WriteLine("Overlap at " + i + ":" + j);
                        count++;
                    }
                    else
                    {
                        angleQueue.Enqueue(getAngleInDegrees(i, j));
                        timeQueue.Enqueue(i + ":" + j);
                        if (angleQueue.Count == 3)
                        {
                            double[] angle = new double[3];
                            string[] time = new string[3];
                            angleQueue.ToArray().CopyTo(angle, 0);
                            timeQueue.ToArray().CopyTo(time, 0);
                            angleQueue.Dequeue();
                            timeQueue.Dequeue();
                            if (angle[1] < angle[0] && angle[1] < angle[2])
                            {
                                Console.WriteLine("Overlap between " + time[0] + " and " + time[2]);
                                count++;
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Total Number of Overlaps = " + count);
        }
        public static double getAngleInDegrees(int hour, int minute)
        {
            if (hour > 12)
            {
                hour = hour - 12;
            }
            double angularDisplacementOfMinuteHand = 6 * minute;
            double angularDisplacementOfHourHand = 30 * hour;
            //Additional displacement of hour hand due to the movement of minute hand
            double additionalAngularDisplacementOfHourHand = 0.5 * minute;
            angularDisplacementOfHourHand += additionalAngularDisplacementOfHourHand;
            double angle = Math.Abs(angularDisplacementOfMinuteHand - angularDisplacementOfHourHand);
            if (angle > 180)
            {
                return (360.0 - angle);
            }
            else
            {
                return angle;
            }
        }
    }
}
Output:
==============================
Overlap at 0:0
Overlap between 1:4 and 1:6
Overlap between 2:10 and 2:12
Overlap between 3:15 and 3:17
Overlap between 4:21 and 4:23
Overlap between 5:26 and 5:28
Overlap between 6:32 and 6:34
Overlap between 7:37 and 7:39
Overlap between 8:43 and 8:45
Overlap between 9:48 and 9:50
Overlap between 10:54 and 10:56
Overlap at 12:0
Overlap between 13:4 and 13:6
Overlap between 14:10 and 14:12
Overlap between 15:15 and 15:17
Overlap between 16:21 and 16:23
Overlap between 17:26 and 17:28
Overlap between 18:32 and 18:34
Overlap between 19:37 and 19:39
Overlap between 20:43 and 20:45
Overlap between 21:48 and 21:50
Overlap between 22:54 and 22:56
Total Number of Overlaps = 22
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s