python百钱买百鸡-百钱买百鸡的Python实现
“百錢買百雞”是一個很有歷史的問題了,大意就是:小雞半文一只,母雞一文一只,公雞二文一只,現(xiàn)有一百文錢,剛好買了一百只雞,請問小雞、母雞與公雞各有多少只?
如果假設(shè)小雞、母雞和公雞各有a,b,c,那么同時滿足以下兩個條件的均可以符合題意:
a + b +c =100
0.5a + 1b + 2*c = 100
顯然不止一組解,可以用Python進行窮舉,代碼如下:
for i in range(1, 101):
for j in range(1, 101-i):
for m in range(1, 101-i -j):
if i * 0.5+j*1 + m *2 == 100 and i + j + m == 100:
print("chicken(s):{}, hen(s):{}, rooster(s):{}.".format(i, j, m))
測試運行的結(jié)果是:
chicken(s):2, hen(s):97, rooster(s):1.
chicken(s):4, hen(s):94, rooster(s):2.
chicken(s):6, hen(s):91, rooster(s):3.
chicken(s):8, hen(s):88, rooster(s):4.
chicken(s):10, hen(s):85, rooster(s):5.
chicken(s):12, hen(s):82, rooster(s):6.
chicken(s):14, hen(s):79, rooster(s):7.
chicken(s):16, hen(s):76, rooster(s):8.
chicken(s):18, hen(s):73, rooster(s):9.
chicken(s):20, hen(s):70, rooster(s):10.
chicken(s):22, hen(s):67, rooster(s):11.
chicken(s):24, hen(s):64, rooster(s):12.
chicken(s):26, hen(s):61, rooster(s):13.
chicken(s):28, hen(s):58, rooster(s):14.
chicken(s):30, hen(s):55, rooster(s):15.
chicken(s):32, hen(s):52, rooster(s):16.
chicken(s):34, hen(s):49, rooster(s):17.
chicken(s):36, hen(s):46, rooster(s):18.
chicken(s):38, hen(s):43, rooster(s):19.
chicken(s):40, hen(s):40, rooster(s):20.
chicken(s):42, hen(s):37, rooster(s):21.
chicken(s):44, hen(s):34, rooster(s):22.
chicken(s):46, hen(s):31, rooster(s):23.
chicken(s):48, hen(s):28, rooster(s):24.
chicken(s):50, hen(s):25, rooster(s):25.
chicken(s):52, hen(s):22, rooster(s):26.
chicken(s):54, hen(s):19, rooster(s):27.
chicken(s):56, hen(s):16, rooster(s):28.
chicken(s):58, hen(s):13, rooster(s):29.
chicken(s):60, hen(s):10, rooster(s):30.
chicken(s):62, hen(s):7, rooster(s):31.
chicken(s):64, hen(s):4, rooster(s):32.
chicken(s):66, hen(s):1, rooster(s):33.
總的來說,這是一個中小學(xué)生都適宜的好問題情境,對于小學(xué)生可以用來培養(yǎng)數(shù)學(xué)思維,比如小雞不可能是單數(shù),比如小雞與公雞的平均價格應(yīng)該是一文一只。而對于中學(xué)生而言則可以用來鍛煉程序思維。
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python百钱买百鸡-百钱买百鸡的Python实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript 数组拼接打印_巧用
- 下一篇: redhat安装wine教程_可能是最漂