Yesterday saw two interview questions, there are two, the first many people have to answer, the second way but few people answer. I am learning php myself recently, so this article is based on PHP to bring the second analysis today.
Two interview questions are included:
1: There are 100 lights in the hall, and each lamp is numbered, 1-100, respectively. Each lamp is controlled by a switch. (The switch is on, the light is on, then the light is off.) The number of the switch is the same as the lamp being controlled. At the beginning, the lights were all extinguished. Now press the switch according to the following rules.
For the first time, all lights are lit.
For the second time, press the switch of all multiples of 2.
For the third time, press the switch of all multiples of 3.
And so on Nth time, click on the switch of multiples of N.
Asked the 100th time after the press, there are a few lights in the hall is bright.
2: There is a 27 cm of the fine wood rod, in the 3rd centimeter, 7 cm, 11 cm, 17 cm, 23 cm, each of the five positions of an ant. The wooden rod is very thin and cannot pass through an ant at the same time. At first, the ants head to the left or to the right is arbitrary, they will only go forward or turn around, but will not retreat. When any two ants meet, two ants will turn around in the opposite direction at the same time. Suppose the ants can walk a centimeter of distance per second. Write a program that asks all ants to leave the wood pole for the minimum and maximum time.
The first one is relatively simple, but the second way to look at is a headache.
Analyze the problem in a simple way.
Judging from the problem itself, it seems that the position of five ants at the same time is a very confusing one. The last sentence of the question is very useful, and all the ants are left with the maximum time and the minimum time of the wooden pole. Use the thin bar as a horizontal axis. The ant's position has been given. When the last left Ant's position <=0 or >=27, all the ants leave the wooden pole. (It seems to be nonsense.) )
1 meters per second, such a problem is set enough to make people comfortable. After all, the amount of time the ant is moving is equal to the number of miles the ant is moving. (If you consider that all ants leave the wooden pole and continue to maintain the original speed movement). And they are moving at the same time .
The ants are only two in the direction of movement, left or right. Taking into account the actual situation of the axes, if we assume that moving to the right is 1, then the equivalence is shifted to-1 to the left. It is important to consider this step in the two-dollar world of computers.
Well, the front is the cushion, whether you read or understand, the following is a more focused content.
Ask for the maximum time and the minimum time, just as we find the maximum number and the minimum number in a bunch of numbers, this kind of thing should not be difficult. The key is to find the last time to do the comparison. What's the relationship between time and what? From the point of view, it should only be related to the state of the initial movement of each ant . As to the movement of an ant at some point in time, is it necessary for us to tangle? No need. That would only complicate the problem.
There are several types of ants in the initial state 2^5=32. It is obvious that each of these 32 kinds of spending time is counted, using a simple loop to do it.
Attention to the ant movement State is nothing more than two variables: position and Direction . So here I simply introduce two arrays of aRR and B. The former is used to describe the current position of a point, which is used in the current direction. $b [i]
As previously described, the value should only be 1 or 1.
With this in mind, we'll be able to smooth the idea and give the array 'aR- R and B ' gives an initial value. Use of Time 'I′DoFollowRing,EveryOneSecondsEveryOnly ma ant move move when ′ arr[k]= = arr[K−1]′When,ChangeChangePhase with State ′ The value of B[k] '. When all the 'ARR′Of′VALUE′<=0OrStakeholders>=27When,Stop stop ring , return back ′ I '. It uses a lot of loop traversal. Of course, for simplicity, when 'when aRr[ K] ' is no longer on a pole, it can be removed using the unset () function. Finally, you can end the loop by judging ' $arr ' as empty.
After finishing the subject, we must also deal with a small detail, How to generate an array describing the state of the ant motion "$b"?
Can not be manually generated, 5 ants 32 cases, 10 ants 1024 kinds of situations manually generated really egg pain. But you know it. Generate 32 arrays, you cannot use them. So it's easy to think of converting a decimal number to a binary number with a length of 5. Then replace 0 of this binary number with-1. Converts the replaced string to an array.
Paste the corresponding code:
<?phpfor ($j =0; $j <32; $j + +) {$var =sprintf ("%05b", $j); $var =str_replace (' 1 ', ' 1| ', $var); $var =str_replace (' 0 ', ' -1| ', $var); $b =explode (' | ', $var); $res =getres ($b); if (Isset ($min)) {if ($res < $min) {$min = $res; }}else{$min = $res; if (Isset ($max)) {if ($res > $max) {$max = $res; }}else{$max = $res; } print_r ($b); echo "The result is". $res. ' $max = '. $max. ' $min = '. $min; echo "This is the way out of the cards, the rules, step by step, but it is also very hard.
----------------------------------------gorgeous divider line-------------------------------------------------------------------------- -----------------
There is no better idea after I have been so seriously talking nonsense?
That is when the meeting, two ants began to turn around. What if we don't turn around and go straight? What's the difference after they turn around? The result is no difference! Each ant starts to take a baton, meet, two people exchange baton, although ants turn around, but the baton but always go to the initial direction Oh ~ So the problem-solving prospects become extremely clear. Knowing the initial state of an ant, you know how long he's been taking the baton for the last time! As for the baton is not the biological, then you tube. Anyway, the last baton left the pole, and the last ant left the pole.
So the time to get some kind of initial state can also be written like this:
function Getres ($b) { $arr =array (3,7,11,17,23); for ($i = 1;; $i + +) { foreach ($arr as $k = + $val) { $arr [$k]= $val + $b [$k]; if ($arr [$k]>=27) | | ($arr [$k]<=0)] { unset ($arr [$k]); } } if (empty ($arr)) { return $i;}} }
----------------------------------------gorgeous divider line-------------------------------------------------------------------------- -----------------
of course, the problem can be more simplified , even the above code is not used.
The above analysis can be seen as the direct walk of each ant does not affect each other. At the end of the maximum minimum value can actually calculate the distance from each ant to both ends. Form five sets of numbers. (3,24), (7,20), (11,16), (10,17), (4,23) the largest of the 5 numbers in a five-group number of smaller values is the minimum value of the final result. The largest of the five groups of 5 of the larger number in each group is the maximum value of the result. It is easy to see that it is 11 and 24. Why is it? A typical barrel effect. The last ant went out to finish the whole thing. Five ants all the shortest path out, get results can be the fastest, five ants all the longest path out, time-consuming can be the slowest.
PS: There should be no faster ideas.
Finally, thank @randeng for his advice on this issue ~
Algorithm problem, do not understand (turn)