当前位置:网站首页>7-5 走楼梯升级版(PTA程序设计)
7-5 走楼梯升级版(PTA程序设计)
2022-07-06 09:22:00 【编程林黛玉】
走楼梯升级版,你前面有n(n>=1)级阶梯,你一次可以走1级阶梯,也可以一次走2级阶梯,还可以1次走三级阶梯,请问n级阶梯的走法有多少种?
输入格式:
请在这里写输入正整数n(n>=1)。
输出格式:
输出n级阶梯的走法。
输入样例:
在这里给出一组输入。例如:
4
样例">输出样例:
在这里给出相应的输出。例如:
7
代码(Python):
def func(n): #设置一个函数
if n==1 or n==2: #可以一次走1阶楼梯或者2阶楼梯
return n #如果n=1(有1阶楼梯)就有一种走法;n=2(有2阶楼梯)就有两种走法
elif n==3: #可以一次走3阶楼梯
return 4 #如果n=3(有3阶楼梯)就有4种走法
else:
return func(n-1)+func(n-2)+func(n-3) #否则,则利用迭代的思想,在函数中调用自身函数
n=int(input()) #输入楼梯阶数
m=func(n) #调用函数
print("%d"%m,end='') #输出结果
(下面给出我看到这道题到做出来的过程中的思路,或许会对大家有帮助)
思路:一开始看到这道题的时候,我的脑子里冒出来的第一个词是组合。因为题目中已经给出一次只能走1阶、2阶或者3阶楼梯,所以我一开始想通过组合这三种不同的走法来解题,即走x次1阶楼梯,y次2阶楼梯,z次3阶楼梯,然后x+2*y+3*z=n。但是忽略了x,y,z值相同但顺序不同的情况,发现这样写有些麻烦,而且没有想出具体的实现办法。然后我又想到可以用一种“小学生”的办法,比如,小学的时候老师教加法的时候经常会问如:7能分成几和几,我们回答:1和6,2和5,3和4......然后由于本题中一次只能走1阶、2阶或者3阶楼梯,所以你最终将把7分成里面只有1,2,3的若干组合。于是就有了迭代。迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果。每一次对过程的重复称为一次“迭代”,而每一次迭代得到的结果会作为下一次迭代的初始值。这是百度百科给出的迭代概念,在我的理解里,可以理解为“函数里面套用自身函数”。具体到本题中,就是通过迭代,将最终的走的楼梯数都分成1,2,3。于是在上面的函数中就先给出了n为1,2,3时的走法。当n为其他值时,通过迭代,调用自身函数,继续拆分,直到全部拆分为1,2,3为止。下面举例当n为7的时候程序的具体实现,或许可以帮助你加以理解。
上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- MySQL事务及实现原理全面总结,再也不用担心面试
- [hand tearing code] single case mode and producer / consumer mode
- About the parental delegation mechanism and the process of class loading
- 深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
- 3.C语言用代数余子式计算行列式
- [the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
- FAQs and answers to the imitation Niuke technology blog project (III)
- Set container
- Nuxtjs快速上手(Nuxt2)
- 关于双亲委派机制和类加载的过程
猜你喜欢
canvas基础1 - 画直线(通俗易懂)
Relationship between hashcode() and equals()
仿牛客技术博客项目常见问题及解答(三)
Mortal immortal cultivation pointer-1
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
Custom RPC project - frequently asked questions and explanations (Registration Center)
9. Pointer (upper)
2. First knowledge of C language (2)
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
Write a program to simulate the traffic lights in real life.
随机推荐
Comparison between FileInputStream and bufferedinputstream
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
FAQs and answers to the imitation Niuke technology blog project (II)
更改VS主题及设置背景图片
[the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
canvas基础2 - arc - 画弧线
1. First knowledge of C language (1)
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Relationship between hashcode() and equals()
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
自定义RPC项目——常见问题及详解(注册中心)
[中国近代史] 第六章测验
7.数组、指针和数组的关系
3.输入和输出函数(printf、scanf、getchar和putchar)
抽象类和接口的区别
2.C语言初阶练习题(2)
一段用蜂鸣器编的音乐(成都)
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
FAQs and answers to the imitation Niuke technology blog project (III)
FAQs and answers to the imitation Niuke technology blog project (I)