ICode国际青少年编程竞赛- Python-6级训练场-递归入门

ICode国际青少年编程竞赛- Python-6级训练场-递归入门

1、

python 复制代码
def recur(n):
    # 边界条件
    if n<1:
        return
    
    # 额外动作
    Dev.step(n)
    Dev.turnRight()
    
    # 递归调用
    recur(n-1)

recur(8)

2、

python 复制代码
def recur(n):
    # 边界条件
    if n<1:
        return
    
    # 额外动作
    Dev.step(n)
    Dev.turnLeft()
    
    # 递归调用
    recur(n-1)
recur(8)

3、

python 复制代码
def recur(n):
    # base case 
    if n < 3:
        return
    # actions
    Spaceship.step(2)
    Spaceship.turnLeft()
    Spaceship.step(n)
    Spaceship.turnLeft()
    Spaceship.turnLeft()
    Spaceship.step(n)
    Spaceship.turnLeft()
    
    # recursion
    recur(n-1)
 
recur(7)

4、

python 复制代码
def recur(n):
    # base case 
    if n < 0:
        return
    # actions
    Spaceship.step(3)
    Spaceship.turnLeft()
    Spaceship.step(5-n)
    Spaceship.turnLeft()
    Spaceship.turnLeft()
    Spaceship.step(5-n)
    Spaceship.turnLeft()
    
    # recursion
    recur(n-1)
 
recur(3)

5、

python 复制代码
def recur(n):
    # Complete the base case 
    if n < 2: 
        return
    # actions
    Spaceship.step(2)
    Spaceship.turnLeft()
    Spaceship.step(n)
    Spaceship.turnLeft()
    Spaceship.turnLeft()
    Spaceship.step(8)
    Spaceship.turnLeft()
    Spaceship.turnLeft()
    Spaceship.step(8-n)
    Spaceship.turnRight()

    # recursion
    recur(n-1)

recur(6)

6、

python 复制代码
def recur(n):
    # Base case
    if n < 1:
        return

    # fill in the actions
    Dev.step(2)
    Dev.turnRight()
    Dev.step(2)
    Dev.turnLeft()
    # recursion
    recur(n-1)

recur(8)

7、

python 复制代码
def recur(n):
    # Base case
    if n < 0: 
        return
    # actions
    Flyer[n].step(1)
    
    # recursion
    recur(n-1)
recur(6)
Dev.step(8)

8、

python 复制代码
def recur(n):
    # Complete the code
    if n > 6: 
        return
    Flyer[n].step(7-n)
    recur(n+1)
recur(0)
Dev.step(14)

9、

python 复制代码
def recur(n):

    # Base case
    if n < 1:
        return

    # Finish the actions
    Flyer.step(2)
    Dev.turnRight()
    Dev.step(2)
    Dev.step(-2)
    Dev.turnLeft()
    Dev.step(2)

    # recursion
    recur(n - 1)

recur(6)

10、

python 复制代码
def recur(n):
    if n < 0: 
        return
    Spaceship.step(2)
    Dev.step(n)
    Dev.step(-2*n)
    Dev.step(n)
    recur(n-2)
Dev.turnRight()
recur(8)

11、

python 复制代码
def recur(n):
    # base case and recursion
    if n < 1: 
        return
    Flyer[7-n].step()
    recur(n-1)
    # actions
    
recur(6)
Dev.step(7)

12、

python 复制代码
def recur(n):
    # base case
    if n > 7: 
        return
    # actions
    Flyer[7-n].step()
    # recursion
    recur(n+1)
recur(0)
Dev.step(9)

13、

python 复制代码
def recur(n):
    # base case
    if n > 3:
        return
    # actions
    Spaceship.step(n+1)
    Flyer[n].step(5-n)
    Dev.step(n+2)
    Dev.step(-n-2)
    # recursion
    recur(n+1)
recur(0)

14、

python 复制代码
def recur(n):
    Dev.step(2)
    if n>0:
        recur(n-1)
    
    Flyer.step(2)
    Dev.turnLeft()
    Dev.step(2)
    Dev.step(-2)
    Dev.turnRight()
    Dev.step(-2)
recur(5)

15、

python 复制代码
def recur(n):
    # base case
    if n < 6: 
        return
    # actions
    Dev.step(n)
    Dev.step(3-n)
    Dev.turnLeft()
    recur(n-1)
    # recursion

recur(9)

16、

python 复制代码
def get(a):
    if a < 1:
        return
    Dev.turnLeft()
    Dev.step(a)
    Dev.turnRight()
    Dev.step(a)
    Dev.step(-a)
    Dev.turnRight()
    Dev.step(a*2)
    Dev.turnLeft()
    Dev.step(a)
    get(a-1)
get(4)

17、

python 复制代码
def move(a):
    if a < 1: 
        return
    Dev.turnRight()
    Dev.step(a)
    Dev.turnLeft()
    Dev.step(a)
    Dev.step(-a)
    Dev.turnRight()
    Dev.step(-2*a)
    Dev.turnLeft()
    Dev.step(3)
    move(a-1)
move(4)

18、

python 复制代码
def move(a):
    if a < 2: 
        return
    Dev.step(a)
    Dev.turnLeft()
    Dev.step()
    Dev.step(-1)
    Dev.turnRight()
    Dev.step(-a)
    Dev.turnLeft()
    Dev.step(a/2)
    Dev.turnRight()
    move(a-2)
move(10)

19、

python 复制代码
def move(a):
    if a < 1: 
        return
    Spaceship.step(a)
    Spaceship.turnRight()
    Spaceship.turnRight()
    Spaceship.step(2*a)
    Spaceship.turnLeft()
    Spaceship.step(a)
    Spaceship.turnLeft()
    move(a-1)
move(4)

20、

python 复制代码
def move(a):
    if a > 5: 
        return
    Spaceship.step(a)
    Spaceship.turnLeft()
    Spaceship.step(a)
    Dev.step(a-1)
    Dev.step(1-a)
    Dev.turnLeft()
    Dev.step(a)
    Dev.turnRight()
    Dev.step(a)
    Dev.step(-a)
    Dev.turnLeft()
    Dev.step(-a)
    Dev.turnRight()
    Spaceship.turnRight()
    move(a+1)
move(2)
相关推荐
大模型铲屎官17 分钟前
【Python-Day 14】玩转Python字典(上篇):从零开始学习创建、访问与操作
开发语言·人工智能·pytorch·python·深度学习·大模型·字典
yunvwugua__19 分钟前
Python训练营打卡 Day27
开发语言·python
Stara05111 小时前
基于多头自注意力机制(MHSA)增强的YOLOv11主干网络—面向高精度目标检测的结构创新与性能优化
人工智能·python·深度学习·神经网络·目标检测·计算机视觉·yolov11
Java致死1 小时前
设计模式Java
java·开发语言·设计模式
zh_xuan1 小时前
c++ 类的语法3
开发语言·c++
那雨倾城2 小时前
使用 OpenCV 将图像中标记特定颜色区域
人工智能·python·opencv·计算机视觉·视觉检测
belldeep5 小时前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言
LuckyTHP5 小时前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
mahuifa6 小时前
(7)python开发经验
python·qt·pyside6·开发经验
学地理的小胖砸8 小时前
【Python 操作 MySQL 数据库】
数据库·python·mysql