hanoi 汉诺塔小游戏编程写步骤

#include<stdio.h>
int main()
{
void hanoi(int n, char one, char two, char three);ide

//声明hanoi函数

int m;
printf("input the number of diskes:\n");
scanf("%d", &m); 函数

//输入要移动盘的数目

printf("The step of move %d diskes\n", m);
hanoi(m, 'A', 'B', 'C'); code

//赋值给hanoi函数

system("pause");
}
void hanoi(int n, char one, char two ,char three) 递归

//定义hanoi函数

{
void move(char x, char y); three

//声明move函数

if (n == 1) input

//递归函数的最后值

move(one, three); it

//赋值给move函数

elseio

//开始递归

{
hanoi(n - 1, one, three, two); class

//赋值给hanoi函数

move(one, three); 移动

//赋值给move函数

hanoi(n - 1, two, one, three);

//赋值给hanoi函数

//这个函数的意思能够看作是:从上往下一共m个盘,

//将m-1个盘所有挪到two上,而后把m盘挪到three上,

//而后再将two上的全部盘挪到three上,就实现了。

//要挪m-1个盘在two上,必须把m-2个盘挪到three上,依次类推,层层递归。

}

//递归结束

}
void move(char x, char y) //定义move函数
{
printf("%c-->%c\n", x, y); //输出每一步的指向
}

//这个hanoi的规律就是,单数的盘,第一个盘先个移向A,双数的第一个盘盘则先移向B。

//移动次数=2^n-1。

相关文章
相关标签/搜索