Puzzles (Programmer interview classic)

NO.1
 20 bottles of pills, 1 bottle of 19 g / grain pills, the remaining bottle with 1.1 g / grain pills. To give you an accurate weighing scales, how to find the heavier bottle of pills? The balance can only be used once.
Solution
Sometimes, strict conditions possible but rather problem-solving clues. In this problem, with the proviso that the balance can only be used once.
Since the balance can only be used once, we are able to know an interesting fact: one must also say a lot of pills, in fact, more precisely, must be weighed from 19 to come up with a pill bottle. Otherwise, if the skip two bottles or more bottles of pills, how to distinguish not weighed that bottles of it? Do not forget, the balance can only be used once.
So, how weighing from multiple bottles of pills, and determine which have relatively heavy bottled pills? Assuming that only two bottles of pills, a bottle of pills which is relatively heavy. Remove the pill bottle, said weight was 2.1 grams, but do not know this extra 0.1 grams from which bottle. We must try to distinguish these bottles.
If you remove the pills from a bottle # 1, # 2 Take two pills from a bottle, then, have to say how much it weighs? The results depend on the circumstances. If a pill bottle # 1 heavier, weighing 3.1 grams was called. If the pill bottle # 2 of heavier, weighing 3.2 grams was called. This is the trick solving this problem.
When said bunch of pills, we will have a "expected" weight. By means of the difference between the expected weight and the actual weight, which can be obtained relatively heavy bottle of pills, provided that the number of different pills taken from each vial.
Two bottles of solution will be promoted before the pills, you can get a complete solution: remove the pills from a bottle # 1, # 2 Remove from two bottles, three removed from the vial # 3, and so on. If the average weight of 1 g each and the pill is said to give a total weight of 210 g (1 + 2 + ... + 20 = 20 * 21/2 = 210), "extra" weight of each piece must come from multiple pills 0.1 g .
The number may be a vial formula (weight - 210 grams) / 0.1 grams stars. Therefore, if this pile of pills was known weight of 211.3 grams, the vial containing the heavier # 13 of pills.

NO.2
 have a chessboard 8 × 8, wherein the upper corner angle of two squares were cut off. Given 31 dominoes, domino may cover exactly two squares. With these 31 domino can cover the entire board? Please justify your answer (provide examples, or prove why can not).
Solution
At first glance, appear to be covered. Board size is 8 × 8, a total of 64 squares, but two boxes have been cut off, so only 62 squares. 31 dominoes should just be able to cover the entire board, right?
Try to cover domino line 1, line 1 while only seven squares, so there must be a domino plated to line 2. The second row is covered with dominoes, we have to be covered by a domino to the third line.
Here Insert Picture Description
To cover each row, there is always a domino must shop to the next line. No matter how many times, how many ways to try, we are unable to successfully spread all dominoes.
In fact, there are more concise and more rigorous proof can not explain why. Haig board originally had 32 and 32 white cells. Two diagonal squares (same color) cut in the corner, only the board 30 and the same color squares 32 squares of another color. For convenience sake of argument, we assume that the remaining 30 Haig and 32 white cells on the board.
Placed on the board to cover each domino bound to a cell and a white Haig. Thus, 31 domino covering exactly 31 and 31 Haig white cells. However, this board only 30 and 32 Haig white cells, therefore, will not cover the entire 31 domino board.

NO.3
 two kettle, capacities of 5 quarts (US: 1 quart = 0.946 liters, Inch: 1 quart = 1.136 liters) and three quarts, unlimited supply of water is (but no cup), how get just four quarts of water with two water bottles? Note that both the kettle irregular shape, can not be accurately filled with "half pot" of water.
Solution
meaning of the questions, we can only use these two water bottles, we might play with some random, shuffling back and forth to the water, you can get a combination of the following order:
Here Insert Picture Description
Note that many puzzles in fact implicitly mathematics or computer science background, this the problem is no exception. As long as these two prime kettle capacity (i.e., two numbers have no common prime factors), we can find a combination of sequential pour, measure out any sum capacity kettle between (including) 1-2 water.

NO.4
 there is a group of people lived on the island, there comes a tourists, set a strange rule: all blue-eyed people have to leave the island as soon as possible. 8:00 every night there will be a flight outlying islands. Everyone can see the color of someone's eye, but do not know their own (nor others can tell). In addition, the island in the end they do not know how many people are blue-eyed, only know at least one person's eyes are blue. All blue-eyed people take days to leave the island?
Solution
will be simple configuration method. Assuming that this island a total of n people, including c people have blue eyes. It is seen from the subject, c> 0.

  1. Case c = 1: only one person is blue-eyed
    assume that everyone on the island are smart, blue-eyed people after four observation, that no one is blue-eyed. But he knew at least one person is blue-eyed, so he must be able to derive the blue-eyed. Therefore, he would take the plane out of the night.
  2. Case c = 2: only two blue eyes of
    two blue-eyed people see each other, not sure c is 1 or 2, but by the previous case, they know that if c = 1, the blue eyes people the first night will outlying islands. So, find another blue-eyed people are still on the island, he will be able to infer that c = 2, which means he is also blue eyes. So, two blue-eyed people will second night in the Islands.
  3. Where c> 2: Generally
    when gradually increase c, we can see that the above described logic and is still applicable. If c = 3, then the three men immediately realized that there are 2-3 people with blue eyes. If there are two blue eyes, the two men will be the second night in the Islands. Therefore, if a second too late the other two are still on the island, every blue-eyed person can infer that c = 3, so this trio has blue eyes. They Islands late in the third.
    Regardless of why the c value, you can apply this model. So, if c is a blue-eyed people, all blue-eyed people can use the c Islands late, and both leave the same evening.

NO.5
 There are 100 high buildings layer. If the higher floor or dropped to the N-th layer, the eggs will be broken. If dropped from the N-th layer below the floor is not broken. Give you two eggs, find N, throwing eggs and the number of requests under the worst-case minimum. (Ask a minimum of good will test how many times you can find the highest floor eggs will not be shattered?) Solution we have found that no matter how throwing eggs 1 (Egg 1), eggs 2 (Egg 2) have to be "broken off between that level and the next highest floor will not be broken "and layer by layer dropped floor (from lowest to highest). For example, if an egg dropped from the floor 10 and the layer 5 is not broken, but broken when dropped layer 15, then, in the worst case, the eggs have to try 2, 12, 13 and the layer 14 dropped floor. Specifically First of all, let's try to start throwing eggs from level 10, then layer 20, and so on.  If the eggs 1 for the first time dropped floor (10 floors) on the rip, then, need to throw up to 10 times.  If the last 1 egg dropped floor (100 level) was broken, then, to throw a maximum of 19 times (10, 20, ..., 90, 100 level, then the 91-99 level). To do so is also very good, but we only considered the absolute worst case. We should be "load balancing", so that the number of throwing eggs in both cases more uniform. Our goal is to design a method of throwing eggs, so throwing eggs 1, either in the first or last time dropped floor was broken, the more stable the number the better. (1) perfect load balancing method should be the number of times throwing eggs 2 1 plus throwing eggs, no matter what time are the same, regardless of the egg is broken when dropped from which floors. (2) If this method throw, throw more than one egg each time, 2 eggs, then it would throw once. (3) Therefore, every throw once the eggs 1, it should be possible to reduce the required number of eggs 2 floor dropped. For example, if an egg layer 20 start to throw down (not broken), then dropped floor layer 30 from the (broken) at this time may have to throw eggs 2 9 times (from 21 to 29 try again). If the egg and then throw again, we must let the number of eggs dropped floor 2 reduced to 8 times. In other words, we have to make an egg dropped from a 39-story building. (4) This indicates that an egg to be thrown down from the X layer begins, then go to increase the X-1 layer until reaching 100 ...... layer. (5) to solve the equation X + (X-1) + (X-2) + ... + 1 = 100, to obtain X (X + 1) / 2 = 100 → X = 14. (Ci provided directly to X, if X and X-1 of these two
Coupled with the X-2 or X times in total, the total number is X) We'll start with 14 layers, layer 27 is then followed by a 39 layer, and so on, in the worst case of eggs to throw 14 times. As solve many other to maximize / minimize the problem, the key problem is that this kind of "balance worst case."

NO.6  
there are 100 on the closing lockers in the hallway. First, there will be 100 individual cabinets are all open. Next, each of a number of two closed cabinet. Then, in the third round, and then switches two switching states of the third cabinet (the cabinet is closed on the opening, on the opened off) intervals. As such operations 100 times repeated regularly in the i-th round, the number of people each i-th to i-th switching state of the cabinet. When the first through the corridor 100, only the state of the switch 100 of the cabinet, the cabinet is open at this time of a few? Solution To solve this problem, we have to figure out the so-called switched lockers switch state what that means. This helps us to infer what the final cabinet was open. 1. Question: Which state cabinet will switch rounds (on or off)? N in each of the cabinet will be a factor of n (including 1 and n itself) that corresponds to a switching state. In other words, the cabinet will be 15 on or off once at 3, 5 and 15. (I = 1 open, closed 3, opening 5, the number 15 off factors: the even off, the odd-Open) 2 problems: When the cabinet is open? If the number of factors (denoted as x) is odd, then the cabinet is open. You can likened to a pair of factors on and off, if left in a factor, the cabinet is open. 3. Question: x What time is odd? If n is a perfect square, then the value of x is odd. The following reasons: The pairing of two complementary factor of n. For example, if n is 36, the pairing of factors: (1, 36), (2, 18), (3, 12), (4, 9), (6, 6). Note that, (6, 6) is only one factor, and therefore the number n is odd factor. 4. Question: How many perfect squares? A total of 10 perfect square, you can count (1,4,9,16,25,36,49,64,81,100), or directly listed square 1-10: 1 1 2 2, 3, 3, ... 10 10 thus, the final total of 10 cabinet was open.

Published 46 original articles · won praise 39 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43518645/article/details/104118528