# python百钱买百鸡问题答案_python 百钱买百鸡 算法的重要性

for hen in range(1,101): #母鸡

for chick in range(1,101): #小鸡

if cock * 5 + hen * 3 + chick == 100:

if cock + hen + chick * 3 == 100:

print("公鸡有%d只\t母鸡有%d只\t小鸡有%d只"%(cock,hen,chick * 3))

for hen in range(3,101 - cock,3): #母鸡

for chick in range(1,101 - cock - hen): #小鸡

if cock // 5 + hen // 3 + chick * 3 == 100 and cock + hen + chick == 100:

print("公鸡有%d只\t母鸡有%d只\t小鸡有%d只" % (cock // 5, hen // 3, chick * 3))

start = time.clock()

for cock in range(1,101): # 公鸡

for hen in range(1,101): #母鸡

for chick in range(1,101): #小鸡

if cock * 5 + hen * 3 + chick == 100 and cock + hen + chick * 3 == 100:

pass

end = time.clock()

time1 = end - start

print("方案一所花时间",time1)

start = time.clock()

for cock in range(5,101,5): # 公鸡

for hen in range(3,101 - cock,3): #母鸡

for chick in range(1,101 - cock - hen): #小鸡

if cock // 5 + hen // 3 + chick * 3 == 100 and cock + hen + chick == 100:

pass

end = time.clock()

time2 = end - start

print("方案二所花时间",time2)

print("方案一所花时间是方案二的%d倍"%(time1 // time2))

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

11-21 178

04-01 3529
10-14
08-27 2万+
12-28 5941
12-07 1291
12-03 1629
03-27 1万+
09-10 328