One interview question: questions about birds and trains

Source: Internet
Author: User

First, let's talk about the context:

Two discussions on this topic in the blog

Http://www.cnblogs.com/beginor/archive/2009/04/27/1444844.html
Http://www.cnblogs.com/ilovemeyou2000/archive/2009/04/29/1445797.html

This "interesting" interview question is: there is a train between the two places 100 kilometers away, and a train leaves the ground for B at a rate of 15 kilometers per hour, another train departs from B to a location at a speed of 20 kilometers per hour. If there is a bird that starts at a speed of 30 kilometers per hour and two trains start at the same time, depart from the ground a, return after encountering another car, and fly back and forth between the two trains in turn, until two trains meet each other. Excuse me,How many times does this bird make a round trip??

First of all, I think there is nothing not rigorous about this question, and there is no obvious misunderstanding. According to the general expression, it is clear that the meaning of the question is to ignore the Turning time of the bird, ignore the length of the bird, and abstract the bird and the locomotive into three points. If I have to consider the Turning time and length of the bird, I think it is possible to misinterpret it intentionally. Any application question has an implicit ignore condition. It is impossible for the respondent to require unlimited and accurate expression of the application question. So I think there is no need to talk about quantum physics. If you want to drill the horns, you can also say that the train road is not a strict straight line, it can be said that the bird is not a constant linear motion, it can be said that according to the relativity speed and distance, time should follow the Lorenz transformation rather than Galileo transformation.

Then there is a solution. According to the original intention, the bird has been round-trip for an unlimited number of times. It can be proved by mathematical induction:

Assume that the k-th car is located at the location of the M bird
When flying to Car B, the two cars are separated.
M-m/(20 + 30) * (15 + 20) = 0.3 m
Then fly to chelaile
0.3 m-0.3 m/(15 + 30) * (15 + 20) = 1/15 m

The distance between the second round trip and the fifth round trip is M/15

Obviously, if M is greater than 0, the distance between the two vehicles after k + 1 is not 0.

At first, the distance between the two vehicles is 100 kilometers rather than 0. Therefore, no matter how many trips the two vehicles are between, the distance between them is not 0.

The answer to this question should have been answered.

However, many friends set a Distance Accuracy for programming. If the distance is smaller than the distance, the two cars are considered to be separated, and then the code implementation is written recursively or iteratively. But programmers are not coders, and not all codes that can be implemented are good. You cannot only know the simulation, regardless of the efficiency.

According to the previous proof, the final round-trip count is only a logarithm operation (note that the round-trip count is used instead of the round-trip count)

Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace bird
{
Class Program
{
Static void main (string [] ARGs)
{
Double exactdistance = 0.000000001;
Console. writeline (convert. toint32 (math. Ceiling (system. Math. Log (exactdistance/1000, 1.0/15 ))));
}
}
}

The last question about infinite time segments was actually raised and solved many years ago: http://en.wikipedia.org/wiki/zeno's_paradoxes

In a similar time-based system, the number of times is not a paradox, and distance is.

Therefore, as an interview question, this question is a very good question. You can answer the question from different perspectives:

1. Basics of mathematics and logical thinking: Check whether the subject understands similar problems and background knowledge, and whether the subject can come up with a solution independently.

2. algorithm optimization Consciousness: Check whether the interviewer only pays attention to the most superficial solution. If you do not want to write recursive Code directly, the programmer must not.

3. ability to understand the problem: Check whether the interviewer can correctly understand the problem and actively communicate with others when there is a deviation to find out the true intention of others. I assume that programmers with a bunch of conditions must not

4. programming language basics: Check whether the interviewer can use a computer language correctly. Programmers who use = to directly compare floating point numbers must not

[Reprinted from http://www.cnblogs.com/winter-cn/archive/2009/04/30/1446636.html]

Certificate ----------------------------------------------------------------------------------------------------------------------------------

Give an IT peer "yugong", a detailed and reasonable explanation, haha.

First, define the "number of trips:
Starting from the beginning, the bird starts from chelaile A, and then every time you meet chelaile A, it counts as a round trip.

Set vehicle a speed to V1, vehicle B speed to V2, and bird speed to V3. The initial distance between the two vehicles is S, and the same initial point of bird and armor. the time when the two cars met was T. The number of times that the birds met with the first car X.
So,
Case 1: V3 <V1, the bird will not appear between the two cars until the two cars meet each other, x = 0
Scenario 2: V3 = V1: the bird and the car a are moving forward at the same speed. When the Car B is encountered, there is no round-trip, and x = 0
Scenario 3: V3> V1 & V3 = V2, laruence will meet chelaile A at the same time as chelaile B, x = 1
Case 3: V3> V1 & V3 <V2. When the two cars meet each other, the bird will not meet chelaile A, x = 0.
Scenario 3: V3> V1 & V3> V2. This is the most complicated situation. The simple logic shows that when the distance between two vehicles is closer, the higher the frequency of the bird's round-trip between the two vehicles, when the distance between the two vehicles approaches 0, the number of bird's round-trips per unit time approaches + ∞, X → + ∞

Do you need to calculate this? Basically, you do not need to calculate it, but it is not.

Case 3 if you want to calculate, you can only give a function with a distance greater than 0 as the parameter, or a function with a time point less than T as the parameter.
When the first K bird encounters a, A and B are separated by S [k];
When k + 1 encounters a, A and B are separated by S [k + 1];

The formula is as follows:

S [k + 1] (v1-v3) (v2-v3)
── ─ = ── ─
S [k] (V1 + V3) (V2 + V3)

That is to say, s [k] is an proportional sequence.
The problem is simple and clear.
I won't say much about the rest.

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.