当前位置:网站首页>Classical questions of function recursion
Classical questions of function recursion
2022-06-23 01:42:00 【'Dream_】
1、 The hanotta problem
The game is on a copper device , There are three rods ( Number A、B、C), stay A From the bottom up 、 Place in order from large to small 64 A gold plate ( Here's the picture ). The goal of the game : hold A All the gold plates on the pole are moved to C On the pole , And still keep the original order . Operating rules : Only one plate can be moved at a time , And keep the big plate down all the time during the moving process , Small in , The plate can be placed during the operation A、B、C On any pole .
analysis : For such a question , No one can write every step of moving the plate directly , But we can use the following methods to solve . Set the number of moving plates to n, To put this n A plate from A The rod moves to C rod , You can do the following three steps :
(1) With C The rod is the intermediary , from A Rod will 1 to n-1 Disk No. moved to B rod ;
(2) take A The remaining third in the rod n Disk No. moved to C rod ;
(3) With A The rod is the intermediary ; from B Rod will 1 to n-1 Disk No. moved to C rod .
------------------------------------------------------------------------------------
Ideas : There is an intermediary , With the help of the target rod , First the (n-1) Put a plate on the intermediary , Put another plate on the target pole ;
namely : that (n-1) A plate is a recursive object .

-------------------------------------------------------------------------------------
Code :
// The hanotta problem -- Find how many times you need to move
//n-1 null : Move (2^(n-1))-1 Time
#include<stdio.h>
#include<math.h>
int ToH(int n)
{
if (n > 0)
{
return (1 + 2 * ToH(n - 1));
}
return (pow(2.0,(double)(n-1.0))-1);
}
int main()
{
int n = 0;
printf(" Please enter the number of plates n:\n");
scanf("%d", &n);
// Hanoi
int ret = ToH(n);
printf("ToH(%d)=%d\n", n, ret);
return 0;
}-------------------------------------------------------------------------------------
2、 The problem of frog jumping on the steps
A frog can jump up at a time 1 Stepped steps , You can jump on it 2 level . Ask the frog to jump on one n How many jumps are there in the steps ( Different results are calculated according to different order ).
----------------------------------------------------------------------------
Ideas : It is also divided into the first step and the rest (n-1) Step problem , Repeat the same work --- recursive
-----------------------------------------------------------------------------
Code :
// The problem of frog jumping on the steps : Find the number of methods
// except 0 1 2 Outside the steps are all Fibonacci sequences
#include<stdio.h>
int Stage(int n)
{
if (0 == n)
{
return 0;
}
else if (1 == n)
{
return 1;
}
else if (2 == n)
{
return 2;
}
else
{
return (Stage(n - 1) + Stage(n - 2));
}
}
int main()
{
int n = 0;
printf(" Please enter the number of steps n:\n");
scanf("%d", &n);
// recursive
int ret = Stage(n);
printf("Kind(%d)=%d\n", n, ret);
return 0;
}---------------------- All a person's anger comes from the pain of his incompetence .-------------------------
边栏推荐
- Const defined variables and for of and for in in JS
- Knowledge point learning
- Detailed explanation of clip attribute parameters
- 9. class and object practice and initialization list
- On function overloading from several examples
- 8. destruct, construct, deep copy, shallow copy, assignment operator overload
- Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
- Zabbix5 series - use temperature and humidity sensor to monitor the temperature and humidity of the machine room (XX)
- JMeter associated login 302 type interface
- How are pub and sub connected in ros1?
猜你喜欢

LeetCode 206. 反转链表(迭代+递归)

3D打印微组织

Debian10 LVM logical volumes

Binary String

SQL programming task04 job - set operation

9. class and object practice and initialization list

Do you know the memory components of MySQL InnoDB?

Development status of full color LED display

Autumn move script B

3. compilation and linking principle
随机推荐
关于打算做一个web的问题看板,需要使用哪些方面语言及数据库知识!
[template] KMP
Wechat mobile terminal development - account login authorization
Pat class a 1016 phone bills (time difference)
leetcode 91. Decode ways (medium)
Pat class A - 1015 reversible primes
Openvino CPU acceleration survey
JS prototype and prototype chain Paramecium can understand
[hdu] p7058 ink on paper finding the maximum edge of the minimum spanning tree
Development status of full color LED display
Steps to implement a container global component
Byte order: big endian vs little endian
Philosopher's walk gym divide and conquer + fractal
C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
Muduo simple usage
Overview of visual object detection technology based on deep learning
Charles garbled code problem solving
It's still like this
Bc117 xiaolele walks up the steps
总体标准差和样本标准差
