frog jumping platform
Principle: A frog jumps n steps, the frog can jump 1 step at a time, or jump 2 steps, ask, how many jumping methods are there, and can jump n steps.
Analysis: Frog jumping is essentially a recursive problem, so why is it a recursive problem?
①If there is a step, the frog can only jump in one way.
②If there are two steps, the frog has two jumping methods, 1. Jumping step by step. 2. Jump two steps
③If there are three steps, the frog has three jumping methods, 1. Jumping step by step. 2. Jump two steps at a time and then jump one step. 3. Jump one step at a time and then two steps.
④If there are four steps, the frog has five jumping methods (I won’t push here anymore, interested friends can push it)
⑤If there are five steps, the frog has nine jumping methods (I won’t push it here, and interested friends can push it)
.......
#include<stdio.h>
int frog_diving_tower(int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
return frog_diving_tower(n - 1) + frog_diving_tower(n - 2);
}
int main()
{
int n = 0;
printf("请输入台阶个数:");
scanf("%d", &n);
int ret = frog_diving_tower(n);
printf("一共有%d种跳法",ret);
return 0;
}
Sublimation: A frog jumps n steps. The frog can jump 1 step at a time, or 2 steps, or 3 steps. Ask, how many jumping methods are there, and can jump n steps.
#include<stdio.h>
int frog_diving_tower(int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 2;
}
if (n == 3)
{
return 3;
}
return frog_diving_tower(n - 1) + frog_diving_tower(n - 2) + frog_diving_tower(n - 3);
}
int main()
{
int n = 0;
printf("请输入台阶个数:");
scanf("%d", &n);
int ret = frog_diving_tower(n);
printf("一共有%d种跳法",ret);
return 0;
}
Note: It is not difficult to see the law, just change the function int frog_diving_tower(int n).
Tower of Hanoi
Principle: There are three pillars A, B, and C, and there are n disks on pillar A. It is required to place the disk of A pillar on disk C, and the small disk is required to be placed on the large disk. Ask: The n disks on the A column are placed on the C disk, how many times do they need to be moved, and find the minimum number of times.
Analysis: The Tower of Hanoi problem is essentially a recursive problem, so why is it a recursive problem?
① If there is a disc: it needs to be moved once. AC.
②If there are two discs: need to move 3 times. AB, AC, BC
③If there are three discs: need to move 7 times. A -> C, A -> B, C -> B, A -> C, B -> A, B -> C, A -> C
......
1683443499141
Notice:
Code
//这里one是A柱
//这里two是B柱
//这里there是C柱
#include<stdio.h>
void move(char x, char y)
{
printf("%c->%c ", x, y);//这里模拟圆盘移动过程
}
void hanoi(int n, char one, char two, char three)
{
if (n == 1)
move(one, three);
else
{
hanoi(n - 1, one, three, two); //将A柱上n-1个盘子移到B柱上(借助 C)
move(one, three); //将A柱上剩下的1个盘子移到C柱上
hanoi(n - 1, two, one, three); //将B柱上n-1个盘子移到C柱上(借助 A)
}
}
int main()
{
int n = 0;
printf("请输入圆盘个数:");
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
Before you know it, it has come to the end. As a novice, I may not write very well. If there is something wrong, please leave a message to me, thank you.