欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python3实现简单汉诺塔的代码教程

程序员文章站 2024-01-16 22:15:58
python3实现简单汉诺塔的代码教程 #!/usr/bin/env python3 #_*_*_ coding: utf-8 _*_*_ #汉诺塔 # def...

python3实现简单汉诺塔的代码教程

#!/usr/bin/env python3
#_*_*_ coding: utf-8 _*_*_

#汉诺塔

# def hannuota(n,N,M):
    # print("将第%d块从%s移动到第%s" %(n,N,M))
# def nuota(n,A,B,C):
    # if n==1:
        # hannuota(1,A,C)
    # else:
        # nuota(n-1,A,C,B)
        # hannuota(n-1,A,C)
        # nuota(n-1,B,A,C)    

    
def nuota(n,A,B,C):
    if n==1:
        # hannuota(1,A,C)
        print("将第%d块从%s移动到第%s" %(m,1,A,C))
        m = m+1
    else:
        nuota(n-1,A,C,B)
        # hannuota(n-1,A,C)
        print("将第%d块从%s移动到第%s" %((m+1),n,A,C))
        m = m+1
        nuota(n-1,B,A,C)