当前位置:网站首页>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))
}边栏推荐
- 投资场内ETF基金是靠谱吗,场内ETF基金安全吗
- Validaterequest= "false" is a "suggestion collection" for what
- Introduction to data warehouse
- MySQL十种锁,一篇文章带你全解析
- MATLAB basic function length function
- 炼金术(7): 何以解忧,唯有重构
- #795 Div.2 D. Max GEQ Sum 单调栈
- Which securities company is better and safer to open an account for stock speculation
- 认识微信小程序项目的基本组成结构
- plot_ Model error: pydot and graphviz are not installed
猜你喜欢

Deep parsing of kubernetes controller runtime

Redis configuration and optimization of NoSQL

Overview and construction of redis master-slave replication, sentinel mode and cluster

Logging log usage

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

Flutter SliverAppBar全解析,你要的效果都在这了!

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

剑指 Offer 61. 扑克牌中的顺子

Ten MySQL locks, one article will give you full analysis

炼金术(1): 识别项目开发中的ProtoType、Demo、MVP
随机推荐
Function and usage of malloc function in C language
CharSequence初探
[black apple series] m910x perfect black apple system installation tutorial – 2 making system USB disk -usb creation
Latest MySQL advanced SQL statement Encyclopedia
IIC communication protocol for single chip microcomputer
Arduino UNO通过电容的直接检测实现简易触摸开关
快速掌握grep命令及正则表达式
Alchemy (2): why use issue management software
MySQL十种锁,一篇文章带你全解析
Matlb| improved forward push back method for solving power flow of low voltage distribution network
FB、WhatsApp群发消息在2022年到底有多热门?
#795 Div.2 E. Number of Groups set *
Is the stock investment exchange group safe? Is it reliable to open an account for free?
Software engineering job design (1): [personal project] implements a log view page
796 div.2 C. managing history thinking
Request object, response object, session object
Translation (4): matching rules for automatic text completion
Code neatness -- function
免费、好用、强大的开源笔记软件综合评测
Distortion model of SDF learning