当前位置:网站首页>Drinking soda scenario 2: recursion
Drinking soda scenario 2: recursion
2022-07-22 21:33:00 【Lost in code】
Buy soda alone , One yuan, one bottle of soda , Three bottle caps can replace a bottle of soda , Two empty bottles can be exchanged for a soda , ask 20 How much soda can I buy for yuan ?
#python -version: python3
def soda(n, bottle, cap):
print([' The current number of bottles you can drink ', ' Current number of empty bottles remaining ', ' Current number of remaining caps '], [n, bottle, cap])
# Exchange the remaining empty bottles , Add the empty bottle left after drinking the drink
bottle = bottle + n
# Exchange the remaining bottle caps , Add the bottle cap left after drinking the drink
cap = cap + n
# After a drink , When the number of empty bottles is less than 2, At the same time, the cap is smaller than 3 when , Then return directly The number of bottles currently drunk
if bottle < 2 and cap < 3:
return n
else: # Add up all the drinks you drink , And the number of bottles exchanged for drinks , Remaining empty bottles , The number of remaining caps enters recursion
return n + soda(bottle//2+cap//3, bottle % 2, cap % 3) # recursive
def main():
for i in [1, 3, 5, 20]:
# Money = int(input('Please input money: '))
Money = i
print('\n')
print(str(Money) + ' Yuan ')
soda_total = soda(Money, 0, 0)
print(str(Money)+' Yuan '+' The total number of soda bottles you can drink is : '+str(soda_total))
if __name__ == '__main__':
main()
Running results :
Um. ~~~, Empty bottles are not borrowed , The bottle cap is not borrowed
Game Over~~~
边栏推荐
- 蓝桥杯-省赛-翻硬币
- Safety first, where is the strength of Beijing modern i-gmp platform?
- Network Accounting - layered structure, OSI communication process, tcp/ip model and 5-layer reference model
- Script execution mode
- Buuctf breakthrough diary --[mrctf2020]ez_ bypass1
- Web编程入门 1.4 PHP连接数据库 PDO
- Bash basic functions alias and shortcut keys
- sed使用心得
- [lttng learning journey] - trace control -- advanced
- 编译gdb7.11.1报错解决
猜你喜欢
水库河道应急广播系统解决方案
Network Accounting - layered structure, OSI communication process, tcp/ip model and 5-layer reference model
云报传媒广场数字电视转模拟系统设计分享
JUC-6.2-并发容器-CopyOnWriteArrayList
蓝桥杯-递增三元组
Web编程入门 3.1 选择框select
蓝桥杯-省赛-翻硬币
自动化测试Json通用解析器
大喇叭疫情防控广播解决方案
Safety first, where is the strength of Beijing modern i-gmp platform?
随机推荐
Process control
10.系统信息相关命令
Blue bridge cup-k-fold range
Juc-6.3-concurrency container queue
Bash basic function - input / output redirection
相比技术与产品 倾听与意见才是Web3当下的首要任务
Web编程入门 1.2页面跳转
4.文本编辑器
水库防汛泄洪抢险应急广播系统建设4G无线广播模式分析
Juc-7.3-thread collaboration cyclicbarrier
C# Winform开发 文件/文件夹的复制 剪切 粘贴
Double pointer in linked list -- fast and slow pointer
Bash basic functions - wildcards and other special symbols
1.1.2计网——标准化工作机器相关组织,性能指标
Buuctf breakthrough diary 04 -- [strong net cup 2019] casual note 1
蓝桥杯-地宫取宝
Juc-7.1 thread collaboration semaphore
大喇叭疫情防控广播解决方案
Solve the thirdscripterror error of wechat applet
中等专业学校校园广播系统解决方案-职业中专校园IP网络广播应用