当前位置:网站首页>实例028:递归求等差数列
实例028:递归求等差数列
2022-08-02 06:16:00 【懒笑翻】
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
解题:
"""
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群,可扫文末的推广进群哦
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
"""
def age(n):
if n == 1:
return 10 # 如果是第一个人那么就是10岁
return 2 + age(n - 1) # 第n个人就加(n-1)次2
print(age(5)) # 18
结果:
边栏推荐
- APP special test: traffic test
- PHP Warning: putenv() has been disabled for security reasons in phar
- 堡垒机、堡垒机的原理
- August 2022 plan, focusing on ue4 video tutorials
- The second day HCIP
- 笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
- MySQL classic 50 practice questions and the most detailed analysis of the whole network
- MySQL 23 classic interviews hang the interviewer
- 实验7 MPLS实验
- 8/1 思维+扩展欧几里得+树上dp
猜你喜欢
MySQL driver jar package download -- nanny tutorial
CAT1 4G+Ethernet development board Tencent cloud mobile phone WeChat applet display temperature and delivery control
MySQL - 多表查询与案例详解
Go inside the basic knowledge
node安装及环境变量配置
How the Internet of Things is changing the efficiency of city operations
awk语法-01-基础语法(命令、选项、内部变量)
(笔记整理未完成)【图论】图的遍历
数据库概论之MySQL表的增删改查2
MySQL 5.7 安装教程(全步骤、保姆级教程)
随机推荐
MySQL驱动jar包的下载--保姆教程
See the picture to understand | How to choose sales indicators to measure the health of business growth
MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
Xgboost报错ValueError:无效的形状:标签(1650 2)
love
交换--STP协议
Summer Summary (3)
[21天学习挑战赛——内核笔记](一)——设备树的概述(硬件、目标、效果、文件类型)
提交代码流程
zabbix email alarm and WeChat alarm
Technology empowers Lhasa's "lungs", Huawei helps Lalu Wetland Smart Management to protect lucid waters and lush mountains
Leetcode Weekly 304
MySQL Advanced SQL Statements (2)
The second day HCIP
MySQL 23 classic interviews hang the interviewer
(Notes are not completed) [Graph Theory] Traversal of graphs
推出 Space On-Premises (本地部署版) Beta 版!
Connection reset by peer problem analysis
About the local server problem after ue4.27 pixel streaming package
nodejs的安装和全局配置(超详细哦)