当前位置:网站首页>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 .-------------------------
边栏推荐
- Unique in Pimpl_ PTR compilation errors and Solutions
- C language student achievement ranking system
- Questions not written in the monthly contest
- 278. digital combination
- Debian10 installing zabbix5.4
- Pat class A - 1007 maximum subsequence sum
- What aspects of language and database knowledge are needed to build a web Kanban!
- E-R图
- There are animation characters interacting with each other when the mouse slides in the web page
- SQL programming task05 job -sql advanced processing
猜你喜欢

魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择

Zabbix5 series - use temperature and humidity sensor to monitor the temperature and humidity of the machine room (XX)

基于深度学习的视觉目标检测技术综述

3. compilation and linking principle
![Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;](/img/75/d2ad171d49611a6578faf2d390af29.jpg)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;

office2016+visio2016

8. destruct, construct, deep copy, shallow copy, assignment operator overload

Steps to implement a container global component

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

9. class and object practice and initialization list
随机推荐
Pat class A - 1015 reversible primes
Nuxt - auto generate dynamic route bug
C. Unstable String
office2016+visio2016
Pat class A - 1013 battle over cities
Similar to attention NLP
LeetCode 206. Reverse linked list (iteration + recursion)
[hdu] p7058 ink on paper finding the maximum edge of the minimum spanning tree
Debian10 configuring rsyslog+loganalyzer log server
Up the Strip
Pat class A - 1014 waiting in line (bank queuing problem | queue+ simulation)
Webdriver and selenium Usage Summary
Quick sort method
Phantomjs Usage Summary
Questions not written in the monthly contest
SQL programming task03 job - more complex query
Network module packaging
Extend your kubernetes API using the aggregation API
Component development
Module 8 job
