当前位置:网站首页>C language classic practice questions (3) - "Hanoi Tower (Hanoi)"
C language classic practice questions (3) - "Hanoi Tower (Hanoi)"
2022-07-30 09:08:00 【it_NunU】
C语言趣味练习题——汉诺塔
文章目录
汉诺塔介绍
汉诺塔(Hanoi) It is a classic problem that must be solved recursively,It comes from Indian mythology.God created the world when he created it3A diamond pillar,Stack them in order of size from bottom to top on the first column64片黄金圆盘.God commanded the Brahmins to put the disc
Rearrange from the bottom to the second post in order of size,And it is stipulated that only one disc can be moved at a time,在小圆盘上不能放大圆盘.
问:试编写,求解 n(n>1)个圆盘的汉诺塔问题.
一、Problem and method analysis

上图为 n( n>1 )The initial state of the Tower of Hanoi with a disc,Let's start with the simplest analysis
对于只有1个圆盘的汉诺塔问题,We just need to go directlyAA disc on the move toBcan be solved above
move_print(int n,char a,char b);//表示将第n个圆盘从a移动到b上
The function of the above function is to convert the firstn个圆盘从a移动到b上,并打印出来
Next, consider how to use recursionn个圆盘借助于 C 从 A 柱移动到 B 柱上
Hanoi(int n, char c,char b,char c);
接着,in order to find outnThe problem of a disc moving,It needs to be solved by mathematical induction:
将上面的 n-1 The disc is regarded as a whole,意思就是将 n The disc is divided into two parts:上面为 n-1 with the largest disc cap n 个,所以,nThe problem of moving a disc can be viewed as the following diagram
A -> C
A -> B
C -> B
二、Steps and code implementation
1.步骤
根据以上分析,可以将 n The solution of the tower of Hanoi is divided into 3步:
1:调用Hanoi(n-1, a, c, b);Complete the first step above;
2:调用move_print(n, a, b);Complete the second step above;
3:调用Hanoi(n, c, b, a);Complete the third step above.
2.代码实现
代码如下(示例):
#include <stdio.h>
void move_print(int n, char a, char b)
{
printf("第%d个圆盘从 %c 移动到 %c\n", n, a, b);
}
void Hanoi(int n, char a, char b, char c)
{
if (1 == n)
{
move_print(n, a, b); // n为1时,直接由 A 移动到 B
}
else
{
Hanoi(n - 1, a, c, b); // 将第 n-1 个圆盘借助 B 从 A 移动到 C
move_print(n, a, b); // 第 n 个圆盘从 A 移动到 B
Hanoi(n - 1, c, b, a); // 将第 n-1 个圆盘借助 A 从 C 移动到 B
}
}
int main()
{
int n = 0;
printf("Please customize the number of discs:");
scanf("%d", &n);
printf("移动%dThe disc steps are as follows:\n", n);
Hanoi(n, 'A', 'B', 'C');
return 0;
}
3.运行结果图

总结
以上就是CThe realization of the tower of Hanoi, a classic example in the language.(如有错误请指出)
If you find it helpful, give it a like and make it difficult to add a follow. Let's go~(后续会持续更新!)
边栏推荐
猜你喜欢

Flutter 环境变量配置和flutter doctor中的错误解决
【SQL server速成之路】——身份验证及建立和管理用户账户

【蓝桥杯选拔赛真题45】Scratch猫鼠游戏 少儿编程scratch蓝桥杯选拔赛真题讲解

电路分析:运放和三极管组成的恒流源电路

TreeSet parsing

AutoSAR EcuM系列02- Fixed EcuM的状态管理

typescript1 - what is typescript

桌面软件开发框架大赏

RFID固定资产盘点系统给企业带来哪些便利?

Detailed explanation of 4D words: C language three-point chess advanced + N-piece chess recursive dynamic judgment of winning or losing
随机推荐
开关电源波纹的产生、测量及抑制,一篇全搞定!
Dynamic Lead Time Promising
stugc_paper
开创ETC生态建设新格局 JASMINER新一批X4服务器陆续发出
[Unity]UI切换环形滚动效果
What convenience does the RFID fixed asset inventory system bring to enterprises?
【无标题】
用代码收集每天热点内容信息,并发送到自己的邮箱
英语语法-名词性从句
剖析SGI STL空间配置器(_S_refill内存块填充函数)
新手必备!最全电路基础知识讲解
20个电路能懂5个以上,足以证明你在电子行业混过!
剑指offer 48:最长不重复子串
R安装包出现error in rawtochar(block[seq_len(ns)]) :
test4
function (1)
AutoSAR EcuM系列02- Fixed EcuM的状态管理
SRAM与DRAM的区别
SEER数据库中“手术变量(RX SUMM-SURG OTH REG/DIS )”下的字段解释
leetcode-990:等式方程的可满足性