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

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

1、

在这里插入图片描述

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

recur(8)

2、

在这里插入图片描述

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

3、
在这里插入图片描述

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、
在这里插入图片描述

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、

在这里插入图片描述

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、

在这里插入图片描述

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、

在这里插入图片描述

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

8、

在这里插入图片描述

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

9、

在这里插入图片描述

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、
在这里插入图片描述

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、
在这里插入图片描述

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、
在这里插入图片描述

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

13、

在这里插入图片描述

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、
在这里插入图片描述

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、
在这里插入图片描述

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、
在这里插入图片描述

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、
在这里插入图片描述

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、

在这里插入图片描述

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、

在这里插入图片描述

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、
在这里插入图片描述

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)
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青岛少儿编程-王老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值