Serialization 12: the timing, minute, and second needles overlap-seemingly simple interview questions

Source: Internet
Author: User

Problem

In the 24 hours of a day, how many times does the hour, minute, and second hands of the clock overlap? What is the time? How did you calculate it?

Analysis

At first glance, I thought it was very simple, but the answers for various online versions are different. Who is right?

We can think about it like this-when the tortoise and the rabbit race, the slow needle will eventually be more than one lap faster. Then, find the coincidence time of the hour and minute hands, and then check whether there is the same.

It is not difficult here. The key is that there is a trap! Excuse me, if I say "more than one lap", will each lap be exceeded?

First obtain the angular velocity: (Degree/second)

1. hour hand: W1 = 360/12*3600 = 1/120 D/S

2. Split needle: W2 = 360/3600 = 0.1 D/S

3. Seconds: W3 = 360/60 = 6 D/S

Among the three needles, the speed of the fast needle is WF, and the speed of the slow needle is ws. If the needle is in 24 hours a day, convert K to 0, 1, 2, 3 ,... , When n turns, the time for the coincidence slow needle is T, then:

WF * t-K * 360 = ws * t-[WS/WF * k] * 360

T = 360 * (k-[K * WS/WF])/(WF-ws)

The Code is as follows:

List <double> times_overlap (double WF, double ws)
{
VaR n = (INT) (24*3600 * WF/360 );
VaR times = new list <double> (N );
For (int K = 0; k <n-1; k ++)
{
VaR T = 360 * (k-(INT) (K * WS/WF)/(WF-ws );
// T = math. Round (t );
If (times. Count = 0 | Times [times. Count-1]! = T) times. Add (t );
}
Return times;
}

Run the program (the displayed time is rounded up in seconds ):

Times_overlap (W2, W1 ):

(1) 00:00:00, (2) 01:05:27, (3) 02:10:54, (4) 03:16:21, (5) 04:21:49, (6) 05:27:16, (7) 06:32:43, (8) 07:38:10, (9) 08:43:38, (10) 09:49:05, (11) 10:54:32, (12) 12:00:00, (13) 13:05:27, (14) 14:10:54, (15) 15:16:21, (16) 16:21:49, (17) 17:27:16, (18) 18:32:43, (19) 19:38:10, (20) 20:43:38, (21) 21:49:05, (22) 22:54:32,

The timestamp times_overlap (W3, W2) of the second-hand coincidence ):

(1) 00:00:00, (2) 00:01:01, (3) 00:02:02, (4) 00:03:03, (5) 00:04:04, (6) 00:05:05, (7) 00:06:06, (8) 00:07:07, (9) 00:08:08, (10) 00:09:09, (11) 00:10:10, (12) 00:11:11, (13) 00:12:12 ,...... 23:53:53, (1412) 23:54:54, (1413) 23:55:55, (1414) 23:56:56, (1415) 23:57:57, (1416) 23:58:58,

The time when the hour, minute, and second needles overlap:

00:00:00 12:00:00

Discussion

Use times_overlap (W2, W1) to calculate the coincidence time between all the hour hands and the minute hands. Use times_overlap (W3, W2) to calculate the coincidence time between all the minute hands and second hands. A total of two coincidence times can be obtained: 0 and 12.

Note that every time the fast and slow needles overlap at, the next ring of the fast and slow needles will not meet each other. Therefore, the hour hand and the minute hand met 22 times, the hour hand and second hand met 1438 times, and the second hand met 1416 times.

 

Author: Silver source link: http://yishan.cc/blogs/gpww/archive/2009/10/28/1262.aspx

-------------------------------------
The beauty of programming-Microsoft technical interview experience source code:
Http://download.csdn.net/source/1883633 http://www.broadview.com.cn/06074

In addition, please give me a favor and vote for it. Let's take a look at the question most liked by everyone in Chapter 1st "the joy of the game-the question encountered in the game, or the question that everyone thinks is the most interesting.
I would like to thank everyone for choosing ^_^ (you can select any of the following websites to vote)
Csdn: http://vote.csdn.net/VotePost.aspx? Voteid = 6419
Kaixinnet: http://www.kaixin001.com /~ Vote/detail. php? Vid = 6324994 & uid = 57936198
QQ space: http://user.qzone.qq.com/80754098/vote/1021116304

Other articles:
Serialization 1: Catalan)
Serialization 2: String set corresponding to sequence Abab
Serialization 3: Longest Common subsequence
Serialization 4: Calculate string Similarity
Serialization 5: search for qualified Integers
Serialization 6: array Cyclic Displacement
Serialization 7: Tianping Scale Ball
Serialization 8: a dynamic and ordered collection -- challenge the red/black tree
Serialization 9: Find the maximum number of K
Serialization 10: Sorting of a pile of cakes
Serialization 11: Overview of the discrete optimization problem search framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.