Python3 猴子吃桃问题

练笔。

猴子吃桃,每天吃一半,又多吃一个;最后剩一个,问5天前总共有多少个桃子。很简单,不废话,直接上代码。

1 #!/usr/bin/env python3
  2 #coding=utf-8
  3 # version 3.0.7
  4 #循环算法
  5 n = 1
  6 for date in range(5,0,-1):
  7     n=(n+1) <<1
  8 print(n)
  9 
 10 #递归算法
 11 def monkey(peace,count,day):
 12     if count == 0 :
 13         print('%2d days before ,there is %2d peaces' %(day,peace))
 14     else :
 15         peace=peace*2+2
 16         count-=1
 17         monkey(peace,count,day)
 18 if __name__ == '__main__' :
 19     day=int(input('please input the total days:'))
 20     peace=int(input('please input the rest:'))
 21     monkey(peace,day,day)
$ ./猴子吃桃问题.py 
94
please input the total days:5
please input the rest:1
 5 days before ,there is 94 peaces

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值