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

#include<stdio.h>

int main()
{
	void hanoi(int n, char one, char two, char three);   //声明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');                             //赋值给hanoi函数
	system("pause");
}
void hanoi(int n, char one, char two ,char three)        //定义hanoi函数
{
	
	void move(char x, char y);                           //声明move函数
	if (n == 1)                                          //递归函数的最后值
		move(one, three);                                //赋值给move函数
	else
//开始递归
	{
		hanoi(n - 1, one, three, two);                    //赋值给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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值