当前位置:网站首页>golang 猴子吃桃子,求第一天桃子的数量
golang 猴子吃桃子,求第一天桃子的数量
2022-06-27 22:51:00 【Cloud孙文波】
/*
猴子吃桃问题,有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个!以后每天猴子都吃其中的一半,然后再多吃一个。
当到第十天时,想再吃,发现只有一个桃子了。问:最初有多少个桃子
思路分析:
1. 第10天只有一个桃子
2. 第九天有几个桃子 = (第十天的桃子数量 + 1) * 2
3. 规律:第n天的桃子数量 peach(n) = (peach(n + 1) + 1) * 2
*/
func peach(n int) int {
if n > 10 || n < 1 {
fmt.Println("输入的天数不对")
return 0 //返回0表示 输入错误
}
if n == 10 {
return 1
} else {
return (peach(n + 1 ) + 1) * 2
}
}
func main() {
fmt.Println("第一天猴子的桃子的数量为",peach(1))
}边栏推荐
- Taro--- day1--- construction project
- #795 Div.2 E. Number of Groups set *
- IIC communication protocol for single chip microcomputer
- 免费、好用、强大的开源笔记软件综合评测
- 剑指 Offer 65. 不用加减乘除做加法
- 吸顶方案1
- Acwing game 57 [unfinished]
- Alchemy (6): iteratable models and use cases
- Installation and use of Zotero document management tool
- Customize MySQL connection pool
猜你喜欢

云厂商为什么都在冲这个KPI?

Logging log usage

技术的极限(11): 有趣的编程

FB、WhatsApp群发消息在2022年到底有多热门?

Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)

剑指 Offer 65. 不用加减乘除做加法

【无标题】

Redis主从复制、哨兵模式、集群的概述与搭建

剑指 Offer 61. 扑克牌中的顺子
Latest MySQL advanced SQL statement Encyclopedia
随机推荐
为什么要选择不锈钢旋转接头
logging日志的使用
哪个证券炒股开户佣金是最便宜,最安全的
Customize MySQL connection pool
plot_model报错:没有安装pydot, graphviz
Alchemy (6): iteratable models and use cases
Mongodb- install a mongodb database locally on the windows computer
Squid代理服务器(缓存加速之Web缓存层)
手机股票开户安全吗,买股票在哪开户?
Differences and functions between intranet IP and public IP
Oracle数据库的启停
Arduino uno realizes simple touch switch through direct detection of capacitance
攻击队攻击方式复盘总结
Acwing game 57 [unfinished]
Alchemy (2): why use issue management software
What are cookies and the security risks of v-htm
无人机专用滑环定制要求是什么
网页鼠标点击特效案例收集(直播间红心同理)
券商买股票用什么app是比较好的,比较安全的
MySQL十种锁,一篇文章带你全解析