Python Basics - "Hundred Money and Hundred Chickens" introductory logic questions (the suggestions at the beginning are hidden)

I. Introduction

Recently, Brother Latiao found that more and more people are learning Python, but how many people know whether it is suitable for learning Python? After all , programming actually requires strong thinking and logic ability in many cases . If the basic thinking and logic ability is not good, then Latiao has to be persuaded well, unless it is really love and willing to spend more time and energy on others. This is only possible. It’s not that Latiao sells itself and brags about thinking logic. This thing can’t be seen or touched. Most of them are born, so I will update you some logic questions from time to time for Latiao. You can try to brush it! help everyone

Today's theme: "Hundred Money Hundred Chickens", this chicken is not another chicken, we are all serious people~

Buy 100 chickens for 100 yuan, 1 rooster for 5 yuan, 1 hen for 3 yuan, and 3 chickens for 1 yuan. How many roosters, hens and chicks can you buy for 100 yuan?

Feel free to figure out your answers and then look at the detailed explanation, which will be more effective. I guess the answer will be varied

Two: ideas

When encountering such a problem for the first time, it may be difficult for everyone to find a clue for a while. You can first determine a quantity: that is, how many chickens can you buy for 100 yuan at most? If you buy roosters, you can only buy 20 at most, hens can buy 33, and chicks can buy 300, but it should be noted that the number of chickens is also limited to 100, so it is impossible to buy 300 chicks, but we can get Come up with an untimed equation: number of roosters + number of hens + number of chicks = 100. In this way, you can determine the number of one kind of chickens to find the combination of the other two kinds of chickens. This exhaustive way is very suitable for solving with loops.

Three: code

#设定公鸡,母鸡,小鸡
cock = 0
hen = 0
chicken = 0

#通过多层循环来穷举数量组合
#最外层控制遍历公鸡数量
for cock in range(0,21):
    #此层控制遍历母鸡数量
    for hen in range(0,34):
        #此层控制遍历小鸡数量
        for chicken in range(0,101):
            #同时满足百钱白鸡两个条件则输出数量
            if(cock*5+hen*3+chicken/3==100 and cock+hen+chicken==100):
                 print('cock:%s hen:%s chicken:%s'%(cock,hen,chicken))

However, in terms of efficiency, the above algorithm performs a lot of invalid calculations, because the number of chicks has been fixed after the rooster and hen are determined, and there is no need to cycle again.

#通过多层循环来穷举数量组合
#最外层控制遍历公鸡数量
for cock in range(0,21):
    #此层控制遍历母鸡数量
    for hen in range(0,34):
        #小鸡数量为 100-公鸡-母鸡,此处就确定了数量,条件控制可以去掉数量判断
        chicken = 100-cock-hen
        if(cock*5+hen*3+chicken/3==100):
              print('cock:%s hen:%s chicken:%s'%(cock,hen,chicken))

At this time, the number of times of exhaustion has changed from 21*34*101 times to 21*34 times, and the efficiency has been greatly improved. The above is the detailed explanation of the problem of Baiji.

Four: Summary

Although the logic of thinking is innate, I am also a lucky person with spicy noodles, hahahaha, but I think it is not a problem as long as I work hard. If you insist on seeing here, you can see my business card of spicy noodles below. You can come to harass me at any time, never resist, come on everyone~

 

Guess you like

Origin blog.csdn.net/AI19970205/article/details/123734657