C language: Frog jumping platform and Hanoi tower problem

                                                               frog jumping platform

1fd28dab7fb343b68fc590bfc25081d1.webp

 

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)

 .......

666ba2d04b9c4257afa3cd5e85f7fb4a.png

 

#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).6f274cb377264b89bdf6563b15d1021b.png

                                                                  Tower of Hanoi

1661e84ef0064574aaaef468b306d782.webp

 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

2d6d4437ed8e43f692641037e5940bd6.png

 Notice:f3c7095dbe814eba9b112d0dabfe787c.png

 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.7221dfce3a9741758214cd250913d198.pngfbd9f005bc434c6194c5e57df8ebe7ae.png

7c1f82abc6e64bafad623fb77ecfae70.gif

 

 

Guess you like

Origin blog.csdn.net/AAlykk/article/details/130537241