河内塔问题_河内塔的Python程序

河内塔问题

You are challenged for a challenge to find the number of moves required to move a stack of disks from one peg to another peg. Wait for a second, it sounds easy? Let’s find are what is going on and in this article, we are introducing a chapter of "TOWER OF HANOI".

您面临一项挑战,即寻找将一叠磁盘从一个钉移到另一个钉所需的移动次数。 等待一秒钟,听起来很简单? 让我们找到正在发生的事情,在本文中,我们将介绍“ TOWER OF HANOI”一章。

You are given with a stack of n disks on a peg arranges as largest is at the bottom and smallest is at the top. We are required to shift this whole stack to another peg (total three pegs, two are empty initially) with considering the following rule:

您会得到一堆n块磁盘,它们在钉子上排列,最大的在底部,最小的在顶部。 我们需要考虑以下规则,将整个堆栈移动到另一个钉子(总共三个钉子,最初两个是空的):

  1. No larger disk can be placed over a smaller one.

    较大的磁盘不能放在较小的磁盘上。

  2. One disk at a time.

    一次一个磁盘。

The problem is looking easy but it's not. The way we are going to tackle it is recursion. The problem is simple when you see it from recursion perspective.

问题看起来很简单,但事实并非如此。 我们要解决的方法是递归。 从递归角度看时,问题很简单。

Key: The number of steps required to shift the stack is exactly equal to the twice of steps for shifting the stack of one less disk(The largest one) plus one step.

关键:移位堆栈所需的步骤数完全等于移位少一个磁盘(最大的磁盘)加一级的步骤的两倍。

    Consider the case of shifting one disk : T(1) = 1Consider the case of shifting two disk : T(2) = 2*T(1) + 1 = 3Consider the case of shifting three disk : T(3) = 2*T(2) + 1 = 7.... T(n) = 2*T(n-1) + 1

Implementing this formula now in our python program is our next goal of solving this.

现在在我们的python程序中实现此公式是解决此问题的下一个目标。

So here is the code:

所以这是代码:

def hanoi(x):
if x == 1:
return 1
else:
return 2*hanoi(x-1) + 1
x = int(input("ENTER THE NUMBER OF DISKS: "))
print('NUMBER OF STEPS: ', hanoi(x))

Output:

输出:

ENTER THE NUMBER OF DISKS: 5
NUMBER OF STEPS:  31

翻译自: https://www.includehelp.com/python/tower-of-hanoi.aspx

河内塔问题

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377350.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VC6、BC5、G2.9标准分配器一览

目录VC6标准分配器BC5标准分配器G2.9标准分配器VC6标准分配器 VCx中源码可以在电脑路径中找: [D:\Program Files\VisualStudio\Community\VC\Tools\MSVC\14.28.29333\include\xmemory] 不过太多了。大概在837行左右有关于allocator代码。还是先看侯捷PPT上的吧。 …

【转】shell 大括号、圆括号的使用

在这里我想说的是几种shell里的小括号,大括号结构和有括号的变量,命令的用法,如下: PHP 代码:1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:string},${var:string},${var:?string} 5.$((exp)) 6.$(var%pattern),$(var%%pattern),$(va…

css clear属性_CSS中的clear属性

css clear属性CSS | 清除财产 (CSS | clear Property) We know so much about float property and how it is used for styling our web pages. If you do not remember the float property, lets help jog your memory. The float property is used to set the elements in a …

linux find prune排除某目录或文件

http://blog.csdn.net/ysdaniel/article/details/7995681 查找cache目录下不是html的文件 find ./cache ! -name *.html -type f列出当前目录下的目录名,排除includes目录,后面的-print不能少 find . -path ./includes -prune -o -type d -maxdepth 1 -print排除多个目录,”(“…

嵌入式指针embedded pointer的概念以及用法

目录前言概念用法参考前言 在针对一个class写出它的内存管理池以及总结出allocator类(三个版本)中内存管理池的第二个版本中涉及到了一个非常重要的概念:嵌入式指针。同时嵌入式指针也在G2.9版本的alloc中出现。现在整理一下网上的一些用法和概念 概念 嵌入式指针…

CLI配置和编址

实施基本编址方案: 在设计新网络或规划现有网络时,至少要绘制一幅指示物理连接的拓扑图,以及一张列出以下信息的地址表: l 设备名称 l 设计中用到的接口 l IP 地址和子网掩码 l 终端设备(如 PC)的默…

sql语句中的in用法示例_PHP中的循环语句和示例

sql语句中的in用法示例循环 (Loops) Imagine that we need a program that says "hello world" 100 times. Its quite stressful and boring to write the statement -- echo "hello world" — 100 times in PHP. This is where loop statement facilitate…

love2d教程30--文件系统

在游戏里少不了文件操作,在love2d里我们可以直接用lua自带的io函数,如果不熟悉可以先读一下我的lua文件读写。 相对lua,love2d提供了更多的函数, 方便我们操作文件。不过可能处于安全考虑,love2d只允许我们访问两个目录…

std::alloc具体细节

G2.9 std::alloc的缺点: 1、在alloc::deallocate中没有将拿到的内存资源还给操作系统,在多任务中将占用很大资源 2、alloc::deallocate函数没有检查传入的p指针的有效性。在这里它默认p为alloc::allocate取得。 如果p并非alloc::allocate取得&#xf…

修改函数的返回地址

这篇随笔源自今天看的这篇文章http://www.cnblogs.com/bluesea147/archive/2012/05/19/2508208.html 1. 如何修改函数返回地址 今天主要写测试程序思考和验证了一下这个问题&#xff0c;先看一下这个&#xff23;程序 1 #include <stdio.h>2 void foo(){3 int a,…

调试JavaScript代码

JavaScript调试代码 (JavaScript debugging the code) Debugging is the process of finding mistakes or bugs in the program. There are several ways one can debug their JavaScript code. This article will walk you through the strict mode in JavaScript and excepti…

Delphi运算符及优先级

单目运算符 (最高优先级) 取变量或函数的地址(返回一个指针) not 逻辑取反或按位取反 乘除及按位运算符 * 相乘或集合交集 / 浮点相除 div 整数相除 mod 取模 (整数相除的余数) as 程序运行阶段类型转换 (RTTI运算符) and 逻辑或按位求和 shl 按位左移 shr 按位右移 加减运算符…

NotifyMyFrontEnd 函数背后的数据缓冲区(二)

message level 函数pq_putmessage调用 low level 函数 pq_putbytes,pq_putbytes调用 internal_putbytes。 从internal_putbyes上来看&#xff0c;就可以发现其数据发送的机制:有一个小技巧&#xff0c;如果数据缓冲区满了&#xff0c;就发送&#xff0c;否则就先堆在那儿。如果…

从源码角度剖析VC6下的内存分配与切割的运作

目录前言1、heap初始化2、第一次分配内存&#xff0c;计算真正区块大小3、new_region管理中心4、__sbh_alloc_new_group()切割第一次分配好的内存5、开始切割内存前言 malloc与free带来的内存管理是应付小区块的&#xff0c;即SBH(small block heap)&#xff0c;这点也可以从源…

windows常见命令整理(持续更新)

windows常见命令整理 1. 文件1.1. 实时显示文件 logfile.txt 中新添加的内容&#xff08;类似于linux tail -f&#xff09; 2. 网络2.1. netstat 3. 进程和任务3.1. tasklist &#xff08;用于列出当前运行的进程及其详细信息&#xff09;3.2. wmic &#xff08;用于执行各种系…

最长公共子序列求序列模板提_最长公共子序列

最长公共子序列求序列模板提Description: 描述&#xff1a; This question has been featured in interview rounds of Amazon, MakeMyTrip, VMWare etc. 这个问题在亚马逊&#xff0c;MakeMyTrip&#xff0c;VMWare等访谈轮次中都有介绍。 Problem statement: 问题陈述&…

洛必达法则使用条件

使用条件 1、分子分母同趋向于0或无穷大 。 2、分子分母在限定的区域内是否分别可导。 3、当两个条件都满足时&#xff0c;再求导并判断求导之后的极限是否存在&#xff1a;若存在&#xff0c;直接得到答案&#xff1b;若不存在&#xff0c;则说明此种未定式无法用洛必达法则解…

求根号m(巴比伦算法)

巴比伦算法是针对求根号m的近似值情况的&#xff0c;它的思想是这样的&#xff1a; 设根号mX0,则如果枚举有答案X(X<X0)&#xff0c;则m/X>X0,当精度要求不高的时候&#xff0c;我们可以看成Xm/XX0,而如果精度要求比较高&#xff0c;我们只需取X和m/X的平均值作为新的枚举…

Android面试题

http://blog.csdn.net/aomandeshangxiao/article/category/841452 http://www.cppblog.com/life02/category/18316.html转载于:https://www.cnblogs.com/DonkeyTomy/articles/2598673.html

r语言 分类变量 虚拟变量_R语言中的变量

r语言 分类变量 虚拟变量R语言| 变数 (R Language | Variables) In the previous tutorial, we have come across the basic information that stands as a pavement for understanding the R language in depth. Now moving future let us educate ourselves about the concep…