: This article mainly introduces a tough interview question. if you are interested in the PHP Tutorial, you can refer to it. Yesterday I saw two interview questions. There were two questions. The first question was answered by many people, but the second question was rarely answered by anyone. I have recently studied php, so this article is based on php and brings the second analysis today.
Two interview questions are provided:
1: There are 100 lamps in the hall, each of which has a number ranging from 1 to 100. Each lamp is controlled by a switch. (Click the switch, the light is on, and then the light is off. The switch number is the same as that of the controlled lamp .) At the beginning, the light is completely off. Click the switch according to the following rules.
For the first time, light up all the lights.
For the second time, click the switch in multiples of all 2.
For the third time, click the switch of all multiples of 3.
And so on. Next, click the switch that multiples of all N.
After 100th Press times, there are still several lights on in the hall.
2: There is a 27 cm fine wooden pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 17 cm. The wooden pole is very small and cannot pass through an ant at the same time. At the beginning, the head of the Ant Financial system is either left or right. they only move forward or turn their heads, but do not move back. When any two ants meet, they both turn their heads to the opposite direction. It is assumed that the Ant Financial can walk 1 cm away every second. Write a program to find the minimum and maximum time for all ants to leave the wooden pole.
The first one is relatively simple, and the second one is a headache.
Briefly analyze this question.
From the perspective of the question itself, it seems confusing to consider the positions of the five ants at the same time. Fortunately, the last sentence of the question is still very useful. The maximum time and minimum time for all ants to leave the wooden pole. Use the rod as a horizontal axis. The positions of Ant Financial have been given. When the position of the final Ant leaves is <= 0 or> = 27, all the Ant leaves the wooden pole. (This seems nonsense .)
1 meter per second. such questions are comfortable enough. After all, the time value of the ant movement is equal to the distance of the ant movement. (If you consider that all the ants are still moving at the original speed when they leave the wooden pole ). And they are simultaneously moving.
There are only two moving directions for Ant Financial, either left or right. Considering the actual coordinate axis, if we assume that we move to the right to 1, it is equivalent to moving to the left to-1. It is very important to consider the dual world of computers.
Well, the previous step is paving the way. whether you understand it or not, the following is more important.
Finding the maximum time and the minimum time is like finding the maximum number and the minimum number in a pile of numbers. this is not very difficult. The key is to find the last comparison time. What is the relationship between time and time? From the question point of view, it should only be related to the initial motion of each ant. Do we have to worry about the moving status of an ant in a certain period of time? No. This will only complicate the problem.
What are the initial statuses of Ant Financial? 2 ^ 5 = 32. Obviously, each of these 32 types takes time, and you can use a simple loop.
There are only two variables that focus on the moving state of Ant Financial: position and direction. So here I will introduce two arrays $ arr and $ B. The former is used to describe the current position of a point, and the latter is used to the current direction. $ B [I]
As described above, the value must be-1 or 1.
With this in mind, we can give an initial value to the arrays '$ arr' and' $ B. Use the time '$ I' for loop, every second after each ant moves, when '$ arr [$ k] ==$ arr [$ k-1, change the value of the matched status value '$ B [k. When all 'value' of '$ arr' <= 0 or> = 27, stop the loop and return' $ I '. A large number of loop traversal is used. For simplicity, when '$ arr [$ k]' is no longer running, you can use the unset () function to delete it. The loop can end if '$ arr' is null.
After talking about the subject content, we must also deal with a small detail. how can we generate an array "$ B" that describes the ant motion status "?
You cannot manually generate it. five Ant Financial cases are 32, and 10 Ant Financial cases are 1024. But you obviously know that 32 arrays are generated and cannot be used. Therefore, we can easily think of converting the decimal number into a binary number with a length of 5. Then replace 0 in the binary number with-1. Convert the replaced string to an array.
Finally, paste the corresponding code:
$ Max) {$ max = $ res;} else {$ max = $ res;} print_r ($ B); echo "the result is ". $ res. '$ max = '. $ max. '$ min = '. $ min; echo"
";} Echo" the maximum value is ". $ max. "The minimum value is ". $ min; // Obtain the time function getRes ($ B) {$ arr = array (, 23); for ($ I = 1; $ I <100; $ I ++) {foreach ($ arras $ k => $ val) {$ arr [$ k] = $ val + $ B [$ k]; if ($ arr [$ k] ==$ arr [$ k-1]) {$ B [$ k] =-$ B [$ k]; $ B [$ k-1] =-@ $ B [$ k-1];} if ($ arr [$ k]> = 27) | ($ arr [$ k] <= 0) {unset ($ arr [$ k]) ;}} if (empty ($ arr )) {return $ I ;}}}
Php is not good at mathematical operations, but I am not good at mathematical operations. I felt that the code I wrote was barely able to complete the task. We also hope to get your guidance.
The above is a tough interview question, including some content. I hope my friends who are interested in the PHP Tutorial can help me.