Basic algorithm----who poisoned the chief

question

A long time ago there was one of the strongest tribes on the prairie, and the leaders of other tribes would pay tribute to the chief every year on his birthday. One day, 100 people came to pay tribute, and each brought a piece of food. After the chief ate one of the food, he foamed at the mouth and died.

question

The national teacher hopes to know which food is poisonous through the fewest guards and the shortest time.

Parse

Every algorithm problem is not out of thin air. For example, the solution of this problem can be used when we multi-threaded processing tasks, using the fewest threads and obtaining the results in the shortest time.

method

  1. The simplest is 100 kinds of food, 100 guards, try one for each person, the food of the dead guard is poisonous food.

But this basically doesn't give points.

  1. Using the segmentation method, the food is divided into N segments, and M guards conduct experiments, and the number of segments is continuously reduced to obtain poisonous food.

But this solution is that the smaller the number of segments, the longer the time-consuming.

  1. Using binary method, such as 100 foods, 2^7 > 100, so we can find 7 guards, labelled 1-7, each food number is represented by binary, and the position of less than 7 digits is replaced by 0.

7 guards take a bite at the guard at the 1 position in the food binary. If you can locate the 1 position where the guard finally died, you can locate the food number.

For example, the dead guards are: 5, 6, 7, and the binary representation is: 0000111, which means that the food with the food number 7 is poisonous.

Guess you like

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