zoukankan      html  css  js  c++  java
  • python作业

    一、使用工具 IDLE

    二、汉诺塔代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    import turtle
     
      
     
    class Stack:
     
        def __init__(self):
     
            self.items = []
     
        def isEmpty(self):
     
            return len(self.items) == 0
     
        def push(self, item):
     
            self.items.append(item)
     
        def pop(self):
     
            return self.items.pop()
     
        def peek(self):
     
            if not self.isEmpty():
     
                return self.items[len(self.items) - 1]
     
        def size(self):
     
            return len(self.items)
     
      
     
    def drawpole_3():#画出汉诺塔的poles
     
        t = turtle.Turtle()
     
        t.hideturtle()
     
        def drawpole_1(k):
     
            t.up()
     
            t.pensize(10)
     
            t.speed(100)
     
            t.goto(400*(k-1), 100)
     
            t.down()
     
            t.goto(400*(k-1), -100)
     
            t.goto(400*(k-1)-20, -100)
     
            t.goto(400*(k-1)+20, -100)
     
        drawpole_1(0)#画出汉诺塔的poles[0]
     
        drawpole_1(1)#画出汉诺塔的poles[1]
     
        drawpole_1(2)#画出汉诺塔的poles[2]
     
      
     
    def creat_plates(n):#制造n个盘子
     
        plates=[turtle.Turtle() for i in range(n)]
     
        for i in range(n):
     
            plates[i].up()
     
            plates[i].hideturtle()
     
            plates[i].shape("square")
     
            plates[i].shapesize(1,8-i)
     
            plates[i].goto(-400,-90+20*i)
     
            plates[i].showturtle()
     
        return plates
     
      
     
    def pole_stack():#制造poles的栈
     
        poles=[Stack() for i in range(3)]
     
        return poles
     
      
     
    def moveDisk(plates,poles,fp,tp):#把poles[fp]顶端的盘子plates[mov]从poles[fp]移到poles[tp]
     
        mov=poles[fp].peek()
     
        plates[mov].goto((fp-1)*400,150)
     
        plates[mov].goto((tp-1)*400,150)
     
        l=poles[tp].size()#确定移动到底部的高度(恰好放在原来最上面的盘子上面)
     
        plates[mov].goto((tp-1)*400,-90+20*l)
     
      
     
    def moveTower(plates,poles,height,fromPole, toPole, withPole):#递归放盘子
     
        if height >= 1:
     
            moveTower(plates,poles,height-1,fromPole,withPole,toPole)
     
            moveDisk(plates,poles,fromPole,toPole)
     
            poles[toPole].push(poles[fromPole].pop())
     
            moveTower(plates,poles,height-1,withPole,toPole,fromPole)
     
      
     
    myscreen=turtle.Screen()
     
    drawpole_3()
     
    n=int(input("请输入汉诺塔的层数并回车: "))
     
    plates=creat_plates(n)
     
    poles=pole_stack()
     
    for i in range(n):
     
        poles[0].push(i)
     
    moveTower(plates,poles,n,0,2,1)
     
    myscreen.exitonclick()

    三、运行结果(输入层数为5)

  • 相关阅读:
    java-Date类,DateFormat类,Calendar类
    vuex
    vue-router学习之二
    Vue-cli2.0
    ES6学习---箭头函数
    【学习】调用iframe中的方法
    【小技巧】只用css实现带小三角的对话框样式
    【转载】实现a元素href URL链接自动刷新或新窗口打开
    【转载】CSS filter:hue-rotate色调旋转滤镜实现按钮批量生产
    【小技巧】object上显示div
  • 原文地址:https://www.cnblogs.com/0609hlz/p/10611832.html
Copyright © 2011-2022 走看看