当前位置:网站首页>7-5 staircase upgrade (PTA program design)
7-5 staircase upgrade (PTA program design)
2022-07-06 13:56:00 【Programming Lindaiyu】
Take the stairs upgrade , In front of you n(n>=1) Steps , You can walk at one time 1 Steps , You can also walk at one time 2 Steps , just so so 1 Take three steps at a time , Excuse me, n How many kinds of steps are there ?
Input format :
Please write and enter a positive integer here n(n>=1).
Output format :
Output n Step by step .
sample input :
Here's a set of inputs . for example :
4
Examples "> sample output :
Here is the corresponding output . for example :
7
Code (Python):
def func(n): # Set up a function
if n==1 or n==2: # You can walk at one time 1 Stairs or 2 Staircase
return n # If n=1( Yes 1 Staircase ) There is a way to walk ;n=2( Yes 2 Staircase ) There are two ways to walk
elif n==3: # You can walk at one time 3 Staircase
return 4 # If n=3( Yes 3 Staircase ) There is 4 Seed walking method
else:
return func(n-1)+func(n-2)+func(n-3) # otherwise , Then use the idea of iteration , Calling self function in function
n=int(input()) # Enter the stair step
m=func(n) # Call function
print("%d"%m,end='') # Output results
( Here are my thoughts from seeing this problem to working it out , It may be helpful to you )
Ideas : When I first saw this problem , The first word that came out of my mind was combination . Because the title has given that you can only go once 1 rank 、2 Step or 3 Staircase , So at the beginning, I wanted to solve the problem by combining these three different walking methods , That is to say x Time 1 Staircase ,y Time 2 Staircase ,z Time 3 Staircase , then x+2*y+3*z=n. But ignore x,y,z Cases with the same value but different order , I find it troublesome to write like this , And didn't come up with a specific implementation . Then I thought I could use one “ Primary school students ” The way to , such as , When teaching addition in primary school, teachers often ask questions such as :7 Can be divided into several and several , We answer :1 and 6,2 and 5,3 and 4...... Then, because in this question, you can only walk at one time 1 rank 、2 Step or 3 Staircase , So you will finally 7 Only 1,2,3 Several combinations of . So there is iteration . Iteration is the activity of repeating the feedback process , Its purpose is usually to approach the desired goal or result . Every repetition of a process is called a “ iteration ”, The result of each iteration will be taken as the initial value of the next iteration . This is the concept of iteration given by Baidu Encyclopedia , In my understanding , It can be understood as “ Function is applied to its own function ”. Specific to this topic , Through iteration , Divide the final number of stairs into 1,2,3. So in the above function, we first give n by 1,2,3 Walking in time . When n For other values , By iteration , Call its own function , Continue splitting , Until it is completely split into 1,2,3 until . The following example is n by 7 The specific implementation of the program , Maybe it can help you understand .
The above program gives more detailed comments , For novice Xiaobai's reference . The idea of program design or code implementation is not optimal , You are welcome to correct your mistakes or give better ideas .
I am a rookie who wants to be Kunpeng , Everyone's encouragement is my driving force , Welcome to like collection comments !
边栏推荐
- Canvas foundation 1 - draw a straight line (easy to understand)
- [面试时]——我如何讲清楚TCP实现可靠传输的机制
- SRC挖掘思路及方法
- 实验八 异常处理
- ABA问题遇到过吗,详细说以下,如何避免ABA问题
- [modern Chinese history] Chapter 6 test
- [the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
- Meituan dynamic thread pool practice ideas, open source
- 杂谈0516
- Experiment 7 use of common classes
猜你喜欢
Nuxtjs快速上手(Nuxt2)
Mixlab unbounded community white paper officially released
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
4. Branch statements and loop statements
SRC mining ideas and methods
Leetcode.3 无重复字符的最长子串——超过100%的解法
Record a penetration of the cat shed from outside to inside. Library operation extraction flag
撲克牌遊戲程序——人機對抗
. How to upload XMIND files to Jinshan document sharing online editing?
随机推荐
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
实验五 类和对象
[modern Chinese history] Chapter 6 test
[graduation season · advanced technology Er] goodbye, my student days
Why use redis
Safe driving skills on ice and snow roads
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
ABA问题遇到过吗,详细说以下,如何避免ABA问题
实验八 异常处理
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
7-3 构造散列表(PTA程序设计)
canvas基础1 - 画直线(通俗易懂)
Canvas foundation 2 - arc - draw arc
Strengthen basic learning records
Implementation principle of automatic capacity expansion mechanism of ArrayList
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
This time, thoroughly understand the MySQL index
为什么要使用Redis
一段用蜂鸣器编的音乐(成都)
实验四 数组