[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>问题
公鸡五块钱一只,母鸡三块钱一只,小鸡一块钱三只,现在要用一百块钱买一百只鸡,问公鸡、母鸡、小鸡各多少只?总共有多少种方案?
<>思路
<>有题可得两个条件:
1.公鸡X5 + 母鸡X3 + 小鸡/3 = 100
2.公鸡 + 母鸡 + 小鸡 =100
所以编写程序的时候需要满足这个两个条件
<>分析方案
只买公鸡,方案最多有二十种,只买母鸡方案最多有三十三种,只买小鸡方案最多有一百种,但是编写程序的时候因为含头不含尾,所以写程序的时候要多加一
<>编写程序
<>程序一、采用三重循环解决
count = 0 for cock in range(21): for hen in range(35): for chick in range(101):
if cock + hen + chick == 100 and cock * 5 + hen * 3 + chick / 3 == 100: count =
count+ 1 print('公鸡:{}\t 母鸡:{}\t 小鸡:{}\t'.format(cock,hen,chick)) print(
'统计结果:百钱买百鸡总共有{}种方案。'.format(count))
<>运行结果
<>程序二、采用双重循环解决
由于知道购买鸡的总数量,知道了公鸡和母鸡的数量,小鸡的数量也就自然而然的确定了,所以请看下面程序
count = 0 for cock in range(21): for hen in range(35): chick = 100 - cock - hen
if cock + hen + chick == 100: count = count + 1 print('公鸡:{}\t 母鸡:{}\t 小鸡:{}\t'.
format(cock,hen,chick)) print('统计结果:百钱买百鸡总共有{}种方案。'.format(count))
<>运行结果
结果还是一样的,而且第二种方案更简单更简便