Python rabbit poison problem instance analysis, python instance analysis

Source: Internet
Author: User

Python rabbit poison problem instance analysis, python instance analysis

This article analyzes the Python rabbit poison problem. Share it with you for your reference. The specific analysis is as follows:

The problem is roughly as follows: 1000 bottles of colorless and tasteless liquid, one of which is the poison, and the others are the water. Only one drop of the poison mixed with the water can also poison the rabbit. Now there are 10 rabbits. When the rabbit dies after drinking the poison for two hours, design a solution to find the bottle of poison within 24 hours.
... 2 minutes later
You must be clear about the previous question. Then, make a slight change: 1000 bottles of colorless and tasteless liquid, one of which is poison, and the others are clear water, only one drop of the poison mixed with water can also poison the rabbit. Now there are 10 rabbits. When the rabbit dies after drinking the poison for 20 hours, design a solution to find the bottle of poison within 24 hours.
... 2 minutes later
There are a variety of methods, such as drinking 100 bottles of liquid together for the rabbit every five minutes, according to the order of the rabbit's death, you can determine the bottle.

Is there a better way? Here I only provide the best time method, that is, to find this bottle of poison in 20 hours. Of course, there can also be at least dead rabbits, finding an optimal compromise between dead rabbits and time.
The method is as follows:
Number 10 rabbits 1-10. Each rabbit represents a number. The list is as follows:
1 2 3 4 5... 10
Number 1 2 4 8 16... 512

The bottle is also numbered, 1-1000 in sequence. Well, you should know what I want to do, but I still want to talk about it and write python code to implement it.
I hope that when the rabbit with the serial number Y1, Y2, Y3. .. dies, I can export the serial number X as the poison. For example, if a rabbit numbered 1, 2, and 4 dies, it is known that the number of the rabbit is 1 + 2 + 8 = 11, that is, the bottle numbered 11 is a poison. For example, if a rabbit numbered 512 and 10 dies, the corresponding number of the rabbit is 1 + 8 + 521 = 521, that is, the bottle numbered is a poison.
The problem now is that you need to know which bottles of liquid each rabbit needs to drink with different numbers.

The Python code is as follows:

Def main (): "" baselist is the number of the rabbit from 1 to 10, and result is the liquid to drink for each rabbit. "" baselist = [, 16, 32, 64,128,256,512] result = [[], [], [], [], [], [], [], [], [] for water in range (1,1001): watertmp = water for I in range (9,-1,-1): if (watertmp-baselist [I])> = 0: watertmp-= baselist [I] result [I]. append (water) for I in range (1, 11): print "The Num % d rabit need drink the next liquid" % I + 1, result [I] passif _ name __= = "_ main _": main ()

I hope this article will help you with Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.