|
|

楼主 |
发表于 2021-7-18 08:21:09
|
显示全部楼层
最后,我们不仿来看看国内大学校园学哥学姐们对古代经典《百钱百鸡》问题的另类解法:' P) _" V5 m$ S" F
- for cock in range(0,101,5):# R& q/ Y2 U* N6 y B" K0 J
- for hen in range(0,101 - cock,3):
$ v1 ^2 f8 ]9 p2 ] - chicken=100-cock-hen; h/ j+ H& t* N0 S" ]# B6 x r
- if cock/5+hen/3+chicken*3==100:; v% R9 O+ Z+ x3 x
- print("cock=%d,hen=%d,chicken=%d"%(cock/5,hen/3,chicken*3))
复制代码 比如这样:
" K, k6 n8 A$ h7 M- cock, hen, chick = 0, 0, 0 #数量1 J1 a% P8 ~ y- C
, p. F& U1 T% p+ Q- v0 H( ^- for cock in range(0,21):5 k- U$ D, \/ U! t! q# F+ e
- for hen in range(0,34):, R* _6 r0 K1 i8 G( t
- for chick in range(100 - cock - hen,101,3):9 V- q q5 Y- Y3 X/ G- @9 @/ m$ P e
$ b; D! D9 v# L8 t. P$ F% o8 P; A- total = cock + hen + chick
3 J! l. N" N L& K - total_price = cock*5 + hen*3 + chick/3. i3 l/ d4 L* \, @. N
- $ v; y: ]+ ^1 P$ F% d4 A
- if total == total_price == 100:; T l( l( D3 Q( z" S5 A
- print('cock = %d, hen = %d, chick = %d, 总数 = %d, 总价 = %d'5 W, c9 @& @- ?, G; k
- % (cock, hen, chick, total, total_price))
复制代码 再比如这样:$ a# ]' F& _7 D% O, L# V
- # Initiate the value
3 u. ]8 R$ ]- w, t' G - hen = 0
# ]1 [9 P% h! ~" w. H. o - rooster = 0, j8 }0 A/ }3 Y( G: ^! ?% e
- chick = 08 N N) K" f; I# E1 a3 W
+ U0 r% _& c# E$ t D V& h- # start calculating
) H! S, D8 j: o( f0 D7 Q1 x3 a - for rooster in range(21):6 [6 F9 F( ~) t) \- D% _
- for hen in range(34): y! A5 A- x: v$ {$ D! E
- if (100 - 3 * hen - 5 * rooster) * 3 + hen + rooster == 100:
1 T% S+ u6 p4 i: f# @; S - chick = 100 - hen - rooster
6 b& n5 l6 a" J# c+ J - print(f"rooster is {rooster},"
; y1 L% g% u" z1 N - f"hen is {hen},": F4 e9 g/ V4 g. q- k
- f"chick is {chick}")
复制代码 总结:条条大道通罗马,希望同学们今后通过对算法不断学习,提升自己编程技能!加油! 预告:暑期公益开播第二讲 - 《查找女神手机号》(点击传送门)4 j- A5 k; t# W3 A$ D5 u1 N3 b
|
|