|
|

楼主 |
发表于 2021-7-18 08:21:09
|
显示全部楼层
最后,我们不仿来看看国内大学校园学哥学姐们对古代经典《百钱百鸡》问题的另类解法:6 E# I$ N9 w. S. C! N5 v; V8 z
- for cock in range(0,101,5):; r1 D1 E* o8 D) ^
- for hen in range(0,101 - cock,3):$ k4 L9 V P) u: @" E- @
- chicken=100-cock-hen
0 A3 D! O; t( ^1 o: L; { - if cock/5+hen/3+chicken*3==100:
# q4 a, c( C/ t - print("cock=%d,hen=%d,chicken=%d"%(cock/5,hen/3,chicken*3))
复制代码 比如这样:
0 U# t Z9 D1 X3 j' t. H- cock, hen, chick = 0, 0, 0 #数量) o8 M: {6 [9 L! v* g- m5 B
- ) c. k u9 ]+ S9 |; U# W0 n' x
- for cock in range(0,21):
) N7 l( w4 q( t/ x - for hen in range(0,34):
! v1 m: J8 m" c8 \ - for chick in range(100 - cock - hen,101,3):
1 z. i3 r: N7 J# `1 C1 ^& x7 |/ z' m) } - 1 T5 H& _) p1 r( E! k
- total = cock + hen + chick! x0 z$ @2 }, y. E$ N
- total_price = cock*5 + hen*3 + chick/3# H; J) q ^7 H
- " o7 E# o1 v4 `) u( c/ t. }1 b
- if total == total_price == 100:
2 U, j/ N1 ]# [+ | - print('cock = %d, hen = %d, chick = %d, 总数 = %d, 总价 = %d'3 L, b, Y; u' _( c9 o9 e8 \
- % (cock, hen, chick, total, total_price))
复制代码 再比如这样:
, o: E }# e* X- # Initiate the value
8 A1 O: t! Q4 ~& Y - hen = 0- I f7 K; d B: g
- rooster = 08 S( s- i$ _$ W/ M5 z
- chick = 0
5 Q) H. W: x1 {2 g' n - , R) |9 N$ G! X4 }& o" F
- # start calculating% d: E) o( _- B! J$ {2 i0 p
- for rooster in range(21):6 s& t. a H" Y$ r
- for hen in range(34):
3 j& g% W- u4 w$ v C - if (100 - 3 * hen - 5 * rooster) * 3 + hen + rooster == 100:% D- f0 m5 U* k* R5 F1 f& v; ~ I' Z
- chick = 100 - hen - rooster3 C. L$ z" M0 n2 }) E
- print(f"rooster is {rooster},"
0 A5 V! s5 O& [2 u, o2 [0 q - f"hen is {hen},"
( [8 J$ g" a# {' j# } - f"chick is {chick}")
复制代码 总结:条条大道通罗马,希望同学们今后通过对算法不断学习,提升自己编程技能!加油! 预告:暑期公益开播第二讲 - 《查找女神手机号》(点击传送门)& U! Y, @6 h3 ^ [( j
|
|