You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
2.6 KiB

5 years ago
{"cells":[{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":"# 百钱白鸡问题1只公鸡5元1只母鸡3元3只小鸡1元100元买100只鸡公鸡母鸡小鸡各有多少\n# 经典三元一次方程求解设各有xyz只\n\n# 解法一推断每种鸡花费依次轮询运行时间最短2019-7-24最优方案\n# import time\n# start = time.perf_counter_ns() # 用自带time函数统计运行时长\nfor x in range(0, 101, 5): # 公鸡花费x元在0-100范围包括100步长为5\n for y in range(0, 101 - x, 3): # 母鸡花费y元在0到100元减去公鸡花费钱数步长为3\n z = 100 - x - y # 小鸡花费z元为100元减去x和y\n if x / 5 + y / 3 + z * 3 == 100:\n print(\"公鸡:%d只母鸡%d只小鸡%d只\" % (x / 5, y / 3, z * 3))\n # pass\n# end = time.perf_counter_ns()\n# time1 = end - start\n# print(\"解法一花费时间:\", time1)"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[],"source":"# 解法二:解法和解法一类似\n# 解题思路买一只公鸡花费5元剩余95元(注意考虑到不买公鸡的情况)再买一只母鸡花费3元剩余92元依次轮询下去钱数不断减\n# 少100元不再是固定的。假设花费钱数依次为x、y、z元\nfor x in range(0, 101, 5): # 公鸡花费x元在0-100范围包括100步长为5\n for y in range(0, 101 - x, 3): # 母鸡花费y元在0到100元减去公鸡花费钱数步长为3\n for z in range(0, 101 - x - y):\n if x / 5 + y / 3 + z * 3 == 100 and x + y + z == 100: # 花费和鸡数都是100\n print(\"公鸡:%d只母鸡%d只小鸡%d只\" % (x / 5, y / 3, z * 3))"},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[],"source":"# 解法三:枚举法\n# 解题思路若只买公鸡最多20只但要买100只固公鸡在0-20之间不包括20;若只买母鸡则在0-33之间不包括33;若只买小鸡则在0-100\n# 之间不包括100\nfor x in range(0, 20):\n for y in range(0, 33):\n z = 100 - x - y # 小鸡个数z等于100只减去公鸡x只加母鸡y只\n if 5 * x + 3 * y + z / 3 == 100: # 钱数相加等于100元\n print(\"公鸡:%d只母鸡%d只小鸡%d只\" % (x, y, z))"}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}