当前位置:网站首页>【C】 Frog jumping steps and Hanoi Tower problem (recursion)
【C】 Frog jumping steps and Hanoi Tower problem (recursion)
2022-06-26 09:12:00 【Xin Xiangrong】
Blog home page : Xin Xiangrong
Series column :【 from 0 To 1,C Language learning 】
A short sentence : If you are in full bloom , Butterflies come !
Blog description : Do what you can , Write every blog , Help yourself familiarize yourself with what you have learned , I also hope these contents can help some partners on the way of learning , If errors and deficiencies are found in the article , Also hope to leave a message in the comment area , We communicate progress together !
List of articles
Preface
This blog summarizes two classic problems in recursion , Frogs jump the steps and the tower of Hanoi , use C Language implementation !
One . Frogs jump the steps
subject :
A frog can jump up at a time 1 Stepped steps , You can jump on it 2 Stepped steps . Ask the frog to jump on one n How many jumps are there in the steps .
Their thinking :
n=1, Jump a step , There's only one way to jump ;
n = 2, Jump a step first , Jump another step , Or just jump two steps , There are two ways to jump ;
n>2, The first time a frog jumps , There are two possibilities , Jump one step or two steps , Just add up the jump methods in these two cases , Use a function fib() To achieve a frog jump n How many jumps are there in the steps , So the result is Fib(n - 1) + Fib(n - 2).
Implement with recursion ,
Code :
#include<stdio.h>
int Fib(int n)
{
if (n <= 2)
{
return n;
}
if (n > 2)
{
return Fib(n - 1) + Fib(n - 2);
}
}
int main()
{
printf(" Enter the number of steps n = ");
int n = 0;
scanf("%d", &n);
printf(" Yes %d Jump \n", Fib(n));
return 0;
}
Running results :

Two . Hanoi
Hanoi (Hanoi Tower), Also known as the river tower , From an old Indian legend .
Vatican made three diamond pillars when he created the world , On a column, they are stacked from bottom to top in order of size 64 A golden disk .
Brahman ordered Brahman to rearrange the disc from below on another pillar in order of size .
And stipulate , anytime , You can't zoom in on a small disk , And you can only move one disc at a time between the three pillars . Ask how to operate ?
subject :
Here we implement such a process , Yes A、B、C Three pillars ,A There are... On the column n null , Will this n The plates move to C On the column , Use code to implement the movement process and calculate how many times to move !
Their thinking :
If n=3, Then the moving process of the three plates is as follows :

First of all, will A The top two plates pass through C Move to B;

then A Plate on move to C;

then B The two plates on the go through A Move to C;

about n null , To pass it through B Put it in C On ;
We must first achieve A Put the bottom plate on C On , So let's first implement A above n-1 A plate passes through C Put it in B On , And then A Put the last plate on C On ;

then A Plate on move to C;
Then we need to realize that B The plate on the table passes through A Put it in C On , The implementation process is similar to the above , This procedure encapsulates a function for recursion .
For calculating the number of moves , It can also be implemented recursively , Ideas as follows :
When n = 1 when , Times: 1;
When n > 1 when ,
- The calculation will n-1 A plate from A adopt B Move to C The number of times
- take A The last plate on the moves to C As a
- take B On the plate (n-1 individual ) adopt A Move to C
You can also calculate the number of times according to the following rules :
- Times: 2 ^ n - 1;
Code implementation :
#include<stdio.h>
#include<math.h>
void Hannoi(int n, char A, char B, char C)
{
if (1 == n)
{
printf("%c->%c ", A, C);
}
if (n > 1)
{
Hannoi(n - 1, A, C, B);
printf("%c->%c ", A, C);
Hannoi(n - 1, B, A, C);
}
}
int Count_hannoi(int n)
{
if (1 == n)
return 1;
if(n > 1)
return 2 * Count_hannoi(n - 1) + 1;
}
int main()
{
printf(" Enter how many plates n = ");
int n = 0;
scanf("%d", &n);
Hannoi(n, 'A', 'B', 'C');// Print the movement process
/*printf("\n Need to carry out %d Secondary mobility \n", (int)pow(2,n) - 1);*/ // Law method
printf("\n Need to carry out %d Secondary mobility \n", Count_hannoi(n));// Recursive method
return 0;
}
Running results :

Conclusion
Dear friends , It's fate to see here , I hope my content can bring you a little help , If you can, support it for three times !!! Thank you for coming here , We can learn and communicate together , Progress together !!! come on. !!!

边栏推荐
- Fix the problem that the rich text component of the applet does not support the properties of video cover, autoplay, controls, etc
- 行为树XML文件 热加载
- phpcms小程序接口新增万能接口get_diy.php
- Code de mise en œuvre de l'intercepteur et du filtre
- Self taught neural network series - 1 Basic programming knowledge
- Fast construction of neural network
- MySQL cannot be found in the service (not uninstalled)
- Mongodb分片环境搭建和验证(redis期末大作业)
- 20220623 Adobe Illustrator入门
- SRv6----IS-IS扩展
猜你喜欢

《一周搞定数电》——组合逻辑电路

隐藏式列表菜单以及窗口转换在Selenium 中的应用

报错ImportError: numpy.core.multiarray failed to import

uniapp用uParse实现解析后台的富文本编辑器的内容及修改uParse样式

Slider verification - personal test (JD)

Basic concept and advanced level of behavior tree

外部排序和大小堆相关知识

ThreadLocal

行为树的基本概念及进阶

Phpcms mobile station module implements custom pseudo static settings
随机推荐
Phpcms V9 adds the reading amount field in the background, and the reading amount can be modified at will
Self taught neural network series - 4 learning of neural network
[qnx hypervisor 2.2 user manual]12.2 terminology (II)
常用电路设计
编辑类型信息
Sqoop merge usage
How to use the least money to quickly open the Taobao traffic portal?
行為樹XML文件 熱加載
[QNX Hypervisor 2.2用户手册]12.2 术语(二)
拦截器与过滤器的实现代码
phpcms v9去掉phpsso模块
Basic concept and advanced level of behavior tree
[Matlab GUI] key ID lookup table in keyboard callback
How to convert wechat applet into Baidu applet
[cloud primordial | kubernetes chapter] go deep into the foundation of all things - container (V)
PD快充磁吸移動電源方案
Cookie session and token
Applet realizes picture preloading (picture delayed loading)
Tensor
Nacos注册表结构和海量服务注册与并发读写原理 源码分析
