interesting puzzle

I accidentally saw some school recruiting puzzles in the subscription account, and I thought it was a bit interesting, so I spent some time collecting some, searched it online, and marked it here~~

  1. There are 20 bottles of pills, of which 19 bottles contain 1 gram per pill, and the remaining bottle contains 1.1 gram per pill. Given a balance that weighs accurately, how do you find the heavier bottle of pills? The balance can only be used once.

Solution: Sometimes, strict constraints can be clues to the solution. In this problem, the limitation is that the balance can only be used once. Since the balance can only be used once, we were also able to learn an interesting fact: a lot of pills must be weighed at the same time, in fact, the pills must be weighed from 19 bottles. Otherwise, if two or more bottles of pills are skipped, how can you tell the difference between the unweighed ones? Don't forget, the balance can only be used once.

So how do you weigh pills from multiple vials and determine which vial contains the heavier pill? Suppose there are only two bottles of pills, one of which is heavier. One pill was taken from each bottle and weighed 2.1 grams, but there was no way of knowing which bottle the extra 0.1 grams came from. We have to try to differentiate these vials.

If you take one pill from bottle #1 and two pills from bottle #2, how much does it weigh? Results depend on the situation. If the pill in bottle #1 is heavier, it will weigh 3.1 grams. If the pill in bottle #2 is heavier, it will weigh 3.2 grams. That's the trick to solving this problem.

When weighing a pile of pills, we have an "expected" weight. The difference between the expected weight and the measured weight can tell which bottle of pills is heavier, provided a different number of pills are taken from each bottle.

Generalizing the solution from the previous two bottles of pills gives the complete solution: take one pill from bottle #1, two pills from bottle #2, three pills from bottle #3, and so on. If each pill weighs 1 gram, the total weight is 210 grams (1 + 2 + … + 20 = 20 * 21 / 2 = 210), the "extra" weight must come from the 0.1 gram extra pill . The number of the vial can be derived from the formula (weight – 210 grams) / 0.1 grams. Therefore, if the pile of pills weighs 211.3 grams, then bottle #13 contains the heavier pill.

2. There is an 8×8 checkerboard with two squares cut off on the opposite corners. Given 31 dominoes, one domino covers exactly two squares. Can you cover the entire chessboard with these 31 dominoes? Please justify your answer (provide an example, or justify why it's not possible).

Solution: At first glance, it seems to be covered. The size of the board is 8×8 and there are 64 squares in total, but two of the squares have been cut off, so there are only 62 squares left. 31 dominoes should just cover the entire board, right?
Try to cover row 1 with dominoes, and row 1 has only 7 squares, so one of the dominoes must go to row 2. When we cover row 2 with dominoes, we have to spread another domino to row 3. To cover each row, there is always a domino that must go to the next row. No matter how many times and how many methods we try, we can't succeed in laying all the dominoes.
In fact, there are more concise and rigorous proofs of why it is not possible. The chessboard originally had 32 black squares and 32 white squares. Cut off the two squares (of the same color) on the diagonal corners, leaving only 30 squares of the same color and 32 squares of the other color. For the sake of argument, we assume that there are 30 black squares and 32 white squares left on the board. Every domino placed on the board must cover a white square and a black square. Therefore, 31 dominoes cover exactly 31 white squares and 31 black squares. However, this board has only 30 black squares and 32 white squares, so 31 dominoes do not cover the entire board.

3. There are two kettles with a capacity of 5 quarts (US: 1 quart = 0.946 liters, English: 1 quart = 1.136 liters) and 3 quarts, if the water supply is unlimited (but there is no measuring cup), how to use These two kettles get exactly 4 quarts of water? Note that the two jugs are irregularly shaped and cannot fill a "half jug" of water precisely.

solution

According to the meaning of the question, we can only use these two kettles. Let’s play around and pour the water back and forth, and we can get the following sequence combination:
Note that many puzzles actually imply a background in mathematics or computer science. This question not excluded. As long as the capacities of the two kettles are relatively prime (that is, the two numbers do not have a common prime factor), we can find a sequence combination of pouring water, and measure any number between the sum (inclusive) of the capacities of 1 to 2 kettles. water volume.

4. There was a group of people living on an island. One day a tourist came and made a strange rule: All blue-eyed people must leave the island as soon as possible. There will be a flight leaving the island at 8pm every night. Everyone can see the color of other people's eyes, but not their own (and no one else can tell). Also, they don't know how many people on the island have blue eyes, only that at least one person has blue eyes. How many days will it take for all the blue-eyed people to get off the island?

Solution: The following will use a simple construction method. Suppose there are n people on this island, and c people have blue eyes. As can be seen from the title, c > 0.

  1. Case c = 1: only one person has blue eyes

Assuming everyone on the island is smart, the blue-eyed people look around and see that no one is blue-eyed. But he knew that at least one person had blue eyes, so he could deduce that he must have blue eyes. So he would leave on the plane that night.

2. Case c = 2: Only two people have blue eyes

Two blue-eyed people see each other and are not sure whether c is 1 or 2, but from the previous case, they know that if c = 1, the blue-eyed person will leave the island on the first night. Therefore, finding that another blue-eyed person is still on the island, he must be able to deduce that c = 2, which means that he himself is blue-eyed. So, both blue-eyed people leave the island on the second night.

3. Case c > 2: General case

As we gradually increase c, we can see that the above logic still applies. If c = 3, then the three people will immediately realize that 2 or 3 people have blue eyes. If two people have blue eyes, they will leave the island on the second night. Therefore, if two other people are still on the island after the second night, each blue-eyed person can infer c = 3, so all three have blue eyes. They will leave the island on the third night.

This mode can be applied regardless of the value of c. So, if there are c people with blue eyes, all blue-eyed people can only leave the island in c night, and they all leave the same night.

5. There is a building that is 100 stories high. If dropped from the Nth floor or higher, the egg will break. If thrown from the floor below the Nth floor, it will not break. Given 2 eggs, find N and ask for the least number of eggs tossed in the worst case.

Solution: We found that no matter how you throw Egg 1 (Egg 1), Egg 2 (Egg 2) must be thrown down floor by floor between the "broken floor" and the next highest floor that will not be broken ( from lowest to highest). For example, if egg 1 was dropped from floors 5 and 10 without breaking, but broke when dropped from floor 15, then, in the worst case, egg 2 must attempt to drop from floors 11, 12, 13, and 14 Throw it downstairs.

specific methods:

First, let's try throwing eggs starting at 10 layers, then 20 layers, and so on.

 If egg 1 is broken the first time it is thrown downstairs (10th floor), then it needs to be thrown 10 times at most.

 If egg 1 breaks the last time it is thrown down the stairs (level 100), then it will take a maximum of 19 throws (levels 10, 20, ..., 90, 100, then 91 to 99).

That's fine too, but we've only considered the absolute worst-case scenario. We should do "load balancing" to more evenly throw eggs in both cases.

Our goal is to design a method of throwing eggs so that when egg 1 is thrown, it does not break until it is thrown down the stairs for the first time or the last time. The more stable the number of times, the better.

(1) The method of perfect load balancing should be that the number of times egg 1 is thrown plus the number of times egg 2 is thrown is the same no matter what floor egg 1 is thrown from.

(2) If there is this way of throwing, every time egg 1 is thrown one more time, egg 2 can be thrown one less time.

(3) Therefore, every time egg 1 is dropped, the number of times that egg 2 may need to be dropped should be reduced. For example, if egg 1 is tossed from the 20th floor first and then from the 30th floor, then egg 2 may have to be thrown 9 times. If egg 1 is tossed again, we must reduce the number of times egg 2 is thrown down the stairs to 8. That is, we have to drop egg 1 from the 39th floor.

(4) From this, it can be seen that egg 1 must be thrown down from the X layer, and then add X1 layers up... until it reaches 100 layers.

(5) Solve the equation X + (X1) + (X2) + … + 1 = 100 to get X (X + 1) / 2 = 100 → X = 14.

We start with 14 layers, then 27 layers, then 39 layers, and so on, with the worst case egg tossing 14 times.

As with many other maximization/minimization problems, the key to this type of problem is "balancing the worst case".

6. There are 100 closed lockers in the hallway. Someone first opened all 100 cabinets. Next, close one of every two cabinets you count. Then, in the third round, switch the switch state of the third cabinet every two times (that is, open the closed cabinet and close the open cabinet). Repeat the operation 100 times in this way. In the i-th round, the person will switch the state of the i-th cabinet every time i count. When the 100th round passes through the corridor, only the switch state of the 100th cabinet is switched. How many cabinets are open at this time?

Solution: To solve this problem, we have to figure out what it means to switch the state of the locker switch. This helps us deduce which cabinets are ultimately open.

  1. Question: In which rounds will the cabinet switch state (on or off)?

Cabinet n will switch states in the round corresponding to each factor of n (including 1 and n itself). That is, cabinet 15 will open or close once in rounds 1, 3, 5, and 15.

  1. Question: When is the cabinet still open?

If the number of factors (denoted by x) is odd, the cabinet is open. You can compare a pair of factors to on and off. If there is one factor left, the cabinet is open.

  1. Question: When is x an odd number?

If n is a perfect square, the value of x is odd. The rationale is as follows: pair the two complementary factors of n. For example, if n is 36, the factor pairings are: (1, 36), (2, 18), (3, 12), (4, 9), (6, 6). Note that (6, 6) actually has only one factor, so n has an odd number of factors.

  1. Question: How many perfect squares are there?

There are 10 perfect squares, you can count them (1, 4, 9, 16, 25, 36, 49, 64, 81, 100), or, simply list the squares of 1 to 10:
1 * 1, 2*2, 3*3, …, 10*10

So in the end a total of 10 cabinets are open.

7. Consider a two-player game. The game is played on a round table. Each player has enough coins. They need to take turns placing coins on the table, and only one coin must be placed at a time, the coin is required to be completely inside the table (no part of it is overhanging the table), and it must not overlap the original coin. Whoever has no place to put new coins loses. Does the first mover or the last mover of the game have a winning strategy? What is this strategy?

Answer: The first mover places a coin in the center of the table, and subsequent coins are always placed in a position commensurate with where the last mover just placed it. In this way, as long as the latecomers can put them, the forerunners must also have a place to put them. The first mover wins.

8. Reverse the words (not characters) of an article using linear time and constant additional space.

Answer: Reverse the order of all the characters of the whole article (reverse the characters in the opposite positions from the beginning); then reverse the order of the characters inside each word in the same way. This way, the word order is reversed throughout the article, but the words themselves are turned back.

9. Rotate a string of length n by m bits to the left using linear time and constant additional space (eg, "abcdefg" shifted by 3 bits is "defgabc").

Answer: Cut the string into two halves of m and nm. Reverse the order of the two parts separately, and then reverse the order of the entire string.

10.** A rectangular cake with a rectangular hollow inside the cake. How do you cut a cake into two equal-sized pieces with just one knife? **

Answer: Notice that the lines that bisect the area of ​​the rectangle pass through the center of the rectangle. Draw a line in the center of each of the oversized rectangle and the hollow rectangle. This line obviously divides both rectangles in half, and their differences are of course equal.

11. A rectangular piece of chocolate initially consists of N x M small pieces. You can only break a piece of chocolate into two small rectangles at a time. How many times does it take to break them into N x M 1×1 little chocolates?

Answer: N x M - 1 time is obviously enough. This number is also necessary, because the current number of pieces of chocolate can only increase by one after each breaking, and of course, dividing the chocolate into N x M pieces requires at least N x M – 1 breaking.

12. How to quickly find out how many "1"s there are in the binary representation of a 32-bit integer? In linear time with respect to the number of "1"s?

Answer 1 (linear about the number of digits): for(n=0; b; b >>= 1) if (b & 1) n++;
Answer 2 (linear about the number of "1"): for(n=0 ; b; n++) b &= b-1;

13.** An array of size N, all numbers are positive integers up to N-1. Find the number of repetitions (assuming there is only one) in O(N) time. An array of size N where all numbers are positive integers up to N+1. Find the number that doesn't appear in O(N) time (assuming there is only one). **

Answer: Calculate the sum of all numbers in the array, and then calculate the sum of all numbers from 1 to N-1. The difference between the two is the repeated number. Calculate the sum of all numbers in the array, and then calculate the sum of all numbers from 1 to N+1, the difference between the two is the missing number.

14. Given a line of C language expression, determine whether the given integer is a power of 2.

Answer: (b & (b-1)) == 0

15. How many points on the earth are there from which one goes one mile south, one mile east, one mile north, and then returns exactly to the starting point?

Answer: "North Pole" is a traditional answer, but there are other answers to this question. In fact, there are infinitely many points that satisfy the requirements. All places 1 + 1/(2π) miles away from the South Pole are satisfied, go one mile south to reach the place 1/(2π) away from the South Pole, go one mile east and make a full circle of latitude , then go north and go back to the starting point. In fact, this is still not the whole point of meeting the requirements. Anywhere 1 + 1/(2kπ) from the South Pole is fine, where k can be any positive integer.

16. A and B are on two islands respectively. B is sick, and A has the medicine that B needs. C has a boat and a lockable chest. C is willing to ship things between A and B, but only in boxes. As long as the box is unlocked, C will steal the contents of the box, no matter what is in the box. If A and B each have a lock and a key that can only open their own lock, how should A safely deliver things to B?

Answer: A puts the medicine into the box and locks the box with its own lock. After B gets the box, he adds his own lock to the box. After the box is transported back to A, A removes its own lock. When the box is transported to B, B takes off his lock and gets the medicine.

17. A couple invites N-1 couples to a party (so there are 2N people in total at the party). Everyone shook hands with everyone they didn't know. Then, the host asked everyone else (2N-1 people in total) how many times they each shook hands, and the answers they got were all different. Assuming everyone knows their spouse, how many times did the hostess shake hands?

Answer: The number of handshakes can only be 2N-1 numbers from 0 to 2N-2. Except for the male host, there are 2N-1 people in total, so each number appears exactly once. One person (0) did not shake hands, and one person (2N-2) shook hands with all the other couples. These two people must be a couple, otherwise the latter will shake hands with the former (so the number of handshakes of the former is no longer 0). Except for this couple, one person (1) only shook hands with (2N-2), and one person (2N-3) shook hands with all other couples except (0). These two people must be a couple, otherwise the latter will shake hands with the former (thus the number of handshakes of the former is no longer 1). And so on, until the person who has shaken N-2 times is matched with the person who has shaken N times. At this point, except for the male master and his spouse, everyone else has been paired. According to the exclusion method, the last remaining person with N-1 handshakes is the hostess.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811265&siteId=291194637